-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile_hlasm.py
181 lines (138 loc) · 5.1 KB
/
compile_hlasm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# Written by Folkert van Heusden
# Released in the public domain
# www.vanheusden.com
# With help from http://www2.latech.edu/~acm/helloworld/asm370.html
import sys
from compile_x86 import CompileToX86
# R6 work register
# R7 pointer
class CompileToHLASM(CompileToX86):
loopNr = 0
lnrs = []
functionsFirst = False
doOptimize = False
def header(self):
print('Brainfuck to HLASM compiler.', file=sys.stderr)
def get_name():
return ('hlasm', 'IBM mainframe (system z/zOS) assembly')
def genindent(self, level):
return ' ' * (level * 9)
def emitLInstr(self, label, instr, pars):
print('%-8s %-5s %s' % (label, instr, pars))
def emitInstr(self, instr, pars):
print('%s%-5s %s' % (self.genindent(1), instr, pars))
def invokeFunction(self, funcNr):
self.emitInstr('LA', 'R15,f%d' % funcNr)
self.emitInstr('BALR', 'R14,R15') # branch and link register
def addToDataPtr(self, n, dot, position):
ind = self.genindent(1)
# load address, LA = 12 bit max, LAY = 20 bit max
print('* add to data ptr')
self.emitInstr('AHI', 'R7,%d' % n)
def subFromDataPtr(self, n, dot, position):
ind = self.genindent(1)
print('* sub from data ptr')
self.emitInstr('L', "R6,=F'%d'" % n)
self.emitInstr('SR', 'R7,R6') # FIXME SR werkt niet zoals verwacht
def addToData(self, n, dot, position):
ind = self.genindent(1)
print('* add to data')
self.emitInstr('LLGC', 'R6,0(R7)')
self.emitInstr('AHI', 'R6,%d' % n)
self.emitInstr('STC', 'R6,0(R7)')
def subFromData(self, n, dot, position):
ind = self.genindent(1)
print('* sub from data')
self.emitInstr('LLGC', 'R6,0(R7)')
self.emitInstr('L', "R5,=F'%d'" % n)
self.emitInstr('SR', 'R6,R5')
self.emitInstr('STC', 'R6,0(R7)')
def emitCharacter(self, n, dot):
ind = self.genindent(1)
print('* emit character')
self.emitInstr('LLGC', 'R6,0(R7)')
self.emitInstr('LA', 'R5,BUFFER')
self.emitInstr('STC', 'R6,0(R5)')
self.emitInstr('LA', 'R1,MSGAREA')
for i in range(0, n):
self.emitInstr('SVC', '35')
def startLoop(self, n, position):
for j in range(0, n):
print('* start loop')
loopName = 'WLOOP%d' % self.loopNr
self.loopNr += 1
self.lnrs.append(loopName)
ind = self.genindent(1)
#self.emitInstr('LA', 'R15,%s_e' % loopName)
self.emitLInstr(loopName, 'LLGC', 'R6,0(R7)')
self.emitInstr('C', "R6,=F'0'")
#self.emitInstr('BZR', 'R15')
self.emitInstr('BE', '%sE' % loopName)
def finishLoop(self, n, dot, position):
for j in range(0, n):
print('* end loop')
jb_label = self.lnrs.pop(-1) # jump bakc label
self.emitInstr('J', '%s' % jb_label)
self.emitInstr('LTORG', '')
jb_label_e = jb_label + "E" # break out of while label
self.emitLInstr(jb_label_e, 'NOP', '')
def addComment(self, s):
print('* %s' % s)
def multilineCommentStart(self):
print('*')
def multilineCommentLine(self, s):
print('* %s' % s)
def multilineCommentEnd(self):
print('*')
def emitFunctions(self):
for blkLoop in range(0, len(self.blocks)):
print('f%d NOP' % blkLoop)
self.translate(self.blocks[blkLoop][0], self.blocks[blkLoop][1])
print('%sPR' % self.genindent(1))
def emitProgramTail(self):
print('* program tail')
print(' LTORG')
print('SAVE DS 18A')
print('MSGAREA EQU *')
print(' DC AL2(5)')
print(" DC XL2'00'")
print("BUFFER DC C'!'")
print('data_mem DS 32000C')
print(' END BEGIN')
def emitProgramBootstrap(self, file):
print(f'* This is a translation of "{file}".')
print('R0 EQU 0')
print('R1 EQU 1')
print('R2 EQU 2')
print('R3 EQU 3')
print('R4 EQU 4')
print('R5 EQU 5')
print('R6 EQU 6')
print('R7 EQU 7')
print('R8 EQU 8')
print('R9 EQU 9')
print('R10 EQU 10')
print('R11 EQU 11')
print('R12 EQU 12')
print('R13 EQU 13')
print('R14 EQU 14')
print('R15 EQU 15')
print('')
self.addComments(self.copyrightNotice)
print('')
def emitMainFunction(self):
ind = self.genindent(1)
print('BF START 0')
print('%sPRINT NOGEN' % ind)
print('BEGIN SAVE (14,12)')
print('%sLR R12,R15' % ind)
print('%sBALR R2,0' % ind)
print('%sUSING *,R2' % ind)
print('%sST R13,SAVE+4' % ind)
print('%sLA R11,SAVE' % ind)
print('%sST R11,8(13)' % ind)
print('%sLR R13,R11' % ind)
print('%sLA R7,data_mem' % ind)
self.translate(0, len(self.allCode))
print('%sL R13,SAVE+4' % ind)
print('%sRETURN (14,12),RC=0' % ind)