-
Notifications
You must be signed in to change notification settings - Fork 1
/
HybroLang.py
executable file
·471 lines (436 loc) · 21.7 KB
/
HybroLang.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/usr/bin/env python3
import antlr4
from antlr4 import *
from H2Utils import *
from HybroLang.HybroLangLexer import HybroLangLexer
from HybroLang.HybroLangParser import HybroLangParser
from HybroLang.HybroLangVisitor import HybroLangVisitor
from HybroLang.H2RegisterBank import H2RegisterBank, regCorrespondances
from HybroLang.H2SymbolTable import H2SymbolTable
from HybroLang.H2LabelTable import H2LabelTable
from HybroLang.H2IR2 import *
from HybroGen.GenGeneratorFromDb import GenGeneratorFromDb
H2_RELEASE = "v4.0"
class HybrogenTreeCompiler(HybroLangVisitor):
def __init__(self, inputCompilette, platform, verbose, debug, dbIds):
self.platform = platform
self.archMaster = self.platform["arch"][0]
self.abi = self.platform["abi"]
self.tab = 0
self.verbose = verbose
self.debug = debug
self.Trace()
self.dbIds = dbIds
archMaster = platform["arch"][0]
extList = platform["extension"][0]
abi = platform["abi"]
self.gen = GenGeneratorFromDb (archMaster, extList[0], abi, dbIds, verbose, debug) # Connect to backend only for master architecture
self.printIfVerbose ("HybrogenVisitor for %s Verbose : %s Debug : %s"%(archList, verbose, debug))
if self.verbose:
print ("HybrogenVisitor for %s Verbose : %s Debug : %s"%(archList, verbose, debug))
l = HybroLangLexer(antlr4.InputStream(inputCompilette)) # Initialize ANTLR lexer, tokenizer, parse, tree
s = CommonTokenStream(l)
p = HybroLangParser(s)
syntaxTree = p.compilationunit()
self.tree = self.visit(syntaxTree) # Visit syntax tree & build IR
def gatherOpsAndTypes(self):
return self.IR.gatherOpsAndTypes()
def getTree(self):
cCode = self.IR.generateCodeGeneration (self.sTable, self.regTmp, self.lTable, self.regIn)
return cCode
def printIfVerbose (self, msg):
if self.verbose:
print (msg, file=sys.stderr)
def Trace(self):
fname = sys._getframe(1).f_code.co_name
self.printIfVerbose ("\t"*self.tab + fname)
def visitFndcl(self, ctx:HybroLangParser.FndclContext):
self.Trace()
self.tab += 1
returnType = self.visit (ctx.datatype())
functionName = ctx.Name().getText()
self.visit (ctx.fnprototype())
regOutput = self.regOut.getNextRegister(returnType["arith"])
varType = H2Type(returnType["arith"], returnType["wordLen"], returnType["vectorLen"])
self.sTable.add("h2_outputVarName", varType, regOutput)
self.tab -= 1
def visitLocalvardef(self, ctx:HybroLangParser.LocalvardefContext):
self.Trace()
self.regCurrent = self.regTmp
self.tab += 1
self.visit (ctx.vardcllist())
self.tab -= 1
def visitDatatype(self, ctx:HybroLangParser.DatatypeContext):
self.Trace()
a = self.removeDieze (ctx.typebase().getText())
w = self.removeDieze (ctx.wordlen.getText())
l = self.removeDieze (ctx.vectorlen.getText())
h = H2Type (a, w, l)
return h
def visitFunction(self, ctx:HybroLangParser.FunctionContext):
self.Trace()
self.tab += 1
archMaster = self.platform["arch"][0]
self.sTable = H2SymbolTable(self.archMaster) # Initialize symbol table & register
self.lTable = H2LabelTable(self.archMaster) #
self.IR = H2IR2(self.platform, self.sTable, self.verbose, self.dbIds)
regin, regout, regtmp = self.gen.getRegisterIOT("f")
self.regIn = H2RegisterBank("In", "flt", regin, verbose=self.verbose)
self.regOut = H2RegisterBank("Out", "flt", regout, verbose=self.verbose)
self.regTmp = H2RegisterBank("Tmp", "flt", regtmp, verbose=self.verbose)
regin, regout, regtmp = self.gen.getRegisterIOT("i")
self.regIn.add("int", regin)
self.regOut.add("int", regout)
self.regTmp.add("int", regtmp)
regin, regout, regtmp = self.gen.getRegisterIOT("vi")
self.regIn.add("vint", regin)
self.regOut.add("vint", regout)
self.regTmp.add("vint", regtmp)
regin, regout, regtmp = self.gen.getRegisterIOT("vf")
self.regIn.add("vflt", regin)
self.regOut.add("vflt", regout)
self.regTmp.add("vflt", regtmp)
self.regCurrent = self.regIn
self.hasReturn = False
decl = self.visit(ctx.fndcl()) # Visit function
body = self.visit(ctx.fnbody())
for i in body:
self.IR.addNode(i)
if not self.hasReturn : # Return insn if no return value (void function)
self.IR.addNode(H2Node(H2NodeType.RTN))
self.tab -= 1
return body
def visitFnbody(self, ctx:HybroLangParser.FnbodyContext):
self.Trace()
self.tab += 1
for localVarDef in ctx.localvardef():
self.visit (localVarDef)
IR = self.visit (ctx.actionlist())
if ctx.returnexpr():
IR += self.visit (ctx.returnexpr())
self.tab -= 1
return IR
def visitActionlist(self, ctx:HybroLangParser.ActionlistContext):
self.Trace()
self.tab += 1
IR = []
for action in ctx.action():
insn = self.visit (action)
IR += insn
self.tab -= 1
return IR
def visitAction(self, ctx:HybroLangParser.ActionContext):
self.Trace()
self.tab += 1
IR = []
if ctx.getChildCount() == 2: # Affectexpr
insn = self.visit (ctx.affectexpr(0))
IR += [insn]
elif ctx.getChildCount() == 11 : # For loop
token = str(ctx.children[0])
if token == "for":
treeLoopPrelude = self.visit(ctx.affectexpr(0))
treeLoopCond = self.visit(ctx.condexpr())
treeLoopContinue = self.visit(ctx.affectexpr(1))
treeLoopAction = self.visit(ctx.actionlist(0))
labelPrologue = self.lTable.genLabelName("prologue")
labelBegin = self.lTable.genLabelName("begin")
labelEnd = self.lTable.genLabelName("end")
labelBA = self.lTable.genLabelName("BA")
IR += [H2Node(H2NodeType.LABEL, labelName = labelPrologue)]
IR += [treeLoopPrelude]
IR += [H2Node(H2NodeType.LABEL, labelName = labelBegin)]
treeLoopCond.setTargetName(labelEnd)
treeLoopCond.setSourceName(labelBegin)
IR += [treeLoopCond]
IR += treeLoopAction
IR += [treeLoopContinue]
IR += [H2Node(H2NodeType.LABEL, labelName = labelBA)]
IR += [H2Node(H2NodeType.BA, targetName = labelBegin, sourceName = labelBA)]
IR += [H2Node(H2NodeType.LABEL, labelName = labelEnd)]
elif token == "if":
treeIfCond = self.visit(ctx.condexpr())
treeIfActionTrue = self.visit(ctx.actionlist(0))
treeIfActionFalse = self.visit(ctx.actionlist(1))
labelPrologue = self.lTable.genLabelName("prologue")
labelEndTrue = self.lTable.genLabelName("endtrue")
labelFalse = self.lTable.genLabelName("iffalse")
labelEnd = self.lTable.genLabelName("ifend")
treeIfCond.setTargetName (labelFalse)
treeIfCond.setSourceName (labelPrologue)
IR += [H2Node(H2NodeType.LABEL, labelName = labelPrologue)]
IR += [treeIfCond]
IR += treeIfActionTrue
IR += [H2Node(H2NodeType.LABEL, labelName = labelEndTrue)]
IR += [H2Node(H2NodeType.BA, targetName = labelEnd, sourceName = labelEndTrue)]
IR += [H2Node(H2NodeType.LABEL, labelName = labelFalse)]
IR += treeIfActionFalse
IR += [H2Node(H2NodeType.LABEL, labelName = labelEnd)]
else:
fatalError("visitAction : unknown child count %d"%ctx.getChildCount())
self.tab -= 1
return IR
def visitAffectexpr(self, ctx:HybroLangParser.AffectexprContext):
self.Trace()
self.tab += 1
if ctx.getChildCount() == 3: # Simple affectation
nodeVar = H2Node(H2NodeType.VARIABLE, variableName = ctx.Name().getText())
opType = self.sTable.get(nodeVar.getVariableName())
nodeVar.setOpType(opType)
nodeExpr = self.visit(ctx.unaryexpr(0))
nodeExpr.setOpType(opType)
IRnode = H2Node(H2NodeType.OPERATOR, opName = "=", sonsList = [nodeVar, nodeExpr], opType=opType)
elif ctx.getChildCount() == 6: # Array affectation STORE
arrayName = ctx.Name().getText()
nodeArrayIndex = self.visit(ctx.unaryexpr(0))
nodeValue = self.visit(ctx.unaryexpr(1))
# @ store = @nodeVar + nodeArrayIndex * wordLen / 8
opType = self.sTable.get(arrayName)
wordLen = opType['wordLen']
vectorLen = opType['vectorLen']
nodeVar = H2Node(H2NodeType.VARIABLE, variableName = arrayName)
nodeConst = H2Node(H2NodeType.CONST, constValue = "(%s * %s) / 8" % (vectorLen, wordLen))
nodeMul = H2Node(H2NodeType.OPERATOR, opName = "*", sonsList = [nodeArrayIndex, nodeConst])
nodeAdd = H2Node(H2NodeType.OPERATOR, opName = "+", sonsList = [nodeVar, nodeMul])
zeroNode = H2Node(H2NodeType.CONST, constValue = 0)
IRnode = H2Node(H2NodeType.W, opName = "W", sonsList = [nodeAdd, nodeValue, zeroNode], opType=opType)
else:
fatalError("visitAffectexpr unknown child count %d"%ctx.getChildCount())
self.tab -= 1
return IRnode
def visitCondexpr(self, ctx:HybroLangParser.CondexprContext):
condToNode = {"==": H2NodeType.BNE, "!=": H2NodeType.BEQ,
"<": H2NodeType.BGE, ">=":H2NodeType.BLT,
">": H2NodeType.BLE, "<=": H2NodeType.BGT,
}
nodeLeft = self.visit(ctx.varorvalue(0))
self.visit(ctx.condOperator())
condExpr = ctx.condOperator().getText()
nodeRight = self.visit(ctx.varorvalue(1))
return H2Node(condToNode[condExpr], opName = condExpr, sonsList = [nodeLeft, nodeRight])
typeCorrespondances = {"int":"int","uint":"int","flt":"flt","sint":"int","suint":"int"}
def visitVardcllist(self, ctx:HybroLangParser.VardcllistContext):
self.Trace()
self.tab += 1
dataType = self.visit(ctx.vardcl())
for varobj in ctx.Name():
varName = varobj.getText()
self.sTable.add (varName, dataType)
self.sTable.setRegister (varName, self.regCurrent.getNextRegister(dataType["arith"][0:3]) )
if self.abi == "power" and self.regCurrent == self.regIn:
if dataType["arith"][0:3] == "int":
self.regCurrent.getNextRegister("flt")
elif dataType["arith"][0:3] == "flt":
self.regCurrent.getNextRegister("int")
self.tab -= 1
def visitVardcl(self, ctx:HybroLangParser.VardclContext):
typeCorrespondances = HybrogenTreeCompiler.typeCorrespondances
self.Trace()
self.tab += 1
dataType = self.visit(ctx.datatype())
self.tab -= 1
varName = ctx.Name().getText()
if self.verbose:
print("[visitVarDecl] allocating on \"%s\" bank for variable '%s'" % (self.regCurrent.bankName, varName))
self.sTable.add (varName, dataType)
#arith = typeCorrespondances[dataType["arith"].replace("[]","")]
#self.sTable.setRegister (varName, self.regCurrent.getNextRegister(arith))
if dataType["arith"][-1] != "]":
self.sTable.setRegister (varName, self.regCurrent.getNextRegister(dataType["arith"][0:3]) )
else:
#declaration of pointer -> change datatype to addr
self.sTable.setRegister (varName, self.regCurrent.getNextRegister("int"))
return dataType
def visitVarorvalueArray(self, ctx:HybroLangParser.VarorvalueArrayContext): # LOAD
self.Trace()
self.tab += 1
treeArrayIndex = self.visit(ctx.unaryexpr())
arrayName = ctx.Name().getText()
# @ R = @nodeVar + treeArrayIndex * wordLen / 8
array_opType = self.sTable.get(arrayName)
R_opType = H2Type(array_opType["arith"].replace("[]",""), array_opType["wordLen"], array_opType["vectorLen"])
# NOTE : this only stands true for 32-bit architectures, the width of the data type should be given as
# a parameter
addrOpType = H2Type("int", 32, 1)
nodeVar = H2Node(H2NodeType.VARIABLE, variableName=arrayName, opType=addrOpType)
wordLen = array_opType['wordLen']
vectorLen = array_opType['vectorLen']
nodeConst = H2Node(H2NodeType.CONST, constValue = "(%s * %s) / 8" % (vectorLen, wordLen), opType=addrOpType)
nodeMul = H2Node(H2NodeType.OPERATOR, opName = "*", sonsList = [treeArrayIndex, nodeConst], opType=addrOpType)
nodeAdd = H2Node(H2NodeType.OPERATOR, opName = "+", sonsList = [nodeVar, nodeMul], opType=addrOpType)
self.tab -= 1
return H2Node(H2NodeType.R, opName = "R", sonsList = [nodeAdd], opType=R_opType)
def visitVarorvalueConst(self, ctx:HybroLangParser.VarorvalueConstContext):
self.Trace()
self.tab += 1
constStr = self.removeDieze (ctx.getText())
n = H2Node(H2NodeType.CONST, constValue = constStr)
self.tab -= 1
return n
def removeDieze (self, word:str):
if "#" == word[0]:
return word[1:]
else:
return word
def visitVarorvalueVar(self, ctx:HybroLangParser.VarorvalueVarContext):
self.Trace()
self.sTable.get(ctx.getText()) # Check variable existence in symbol table
return H2Node(H2NodeType.VARIABLE, variableName = ctx.getText())
def visitReturnexpr(self, ctx:HybroLangParser.ReturnexprContext):
self.Trace()
self.tab += 1
nodeRetExpr = self.visit(ctx.unaryexpr())
opType = self.sTable.get("h2_outputVarName")
nodeRetVar = H2Node(H2NodeType.VARIABLE, variableName = "h2_outputVarName", opType=opType)
returnValue = H2Node(H2NodeType.OPERATOR, opName = "=", sonsList = [nodeRetVar, nodeRetExpr], opType=opType)
returnNode = H2Node(H2NodeType.RTN)
self.hasReturn = True
self.tab -= 1
return [returnValue, returnNode]
def visitUnaryexpr(self, ctx:HybroLangParser.UnaryexprContext):
self.Trace()
self.tab += 1
if ctx.getChildCount() == 3:
l = self.visit(ctx.unaryexpr(0))
r = self.visit(ctx.unaryexpr(1))
l_varName = l.getVariableName()
r_varName = r.getVariableName()
# If the LHS and the RHS own variable names, we use them to get their opTypes
# otherwise we default it to None
l_opType = self.sTable.get(l_varName) if l_varName is not None else None
r_opType = self.sTable.get(r_varName) if r_varName is not None else None
l.setOpType(l_opType)
r.setOpType(r_opType)
node = H2Node(H2NodeType.OPERATOR, opName = ctx.op.text, sonsList = [l, r])
else:
node = self.visit(ctx.varorvalue())
self.tab -= 1
return node
def BuildTreeAndCompile (inputCompilette, platform, verbose, debug, dbIds):
v = HybrogenTreeCompiler(inputCompilette, platform, verbose, debug, dbIds)
cCode = v.getTree()
prefixDict = v.gatherOpsAndTypes()
return prefixDict, cCode
def writeCandParseCompilette(fileIn, platform, verbose, debug, dbIds):
""" Interleave C and rewrited compilette
"""
BEGIN = "#["
END = "]#"
outsideCompilette = True
cCode = ""
prefixDict = dict()
for line in fileIn:
if (BEGIN in line and outsideCompilette):
outsideCompilette = False
line, compiletteCode = line.split(BEGIN)
elif END in line and not outsideCompilette:
outsideCompilette = True
endCompilette, line = line.split(END)
compiletteCode += endCompilette
pDict, compiledCompilette = BuildTreeAndCompile(compiletteCode, platform, verbose, debug, dbIds)
for arith, prefixes in pDict.items():
prefixDict.setdefault(arith, set()).update(prefixes)
cCode += str(compiledCompilette)
elif (BEGIN in line and not outsideCompilette) or (END in line and outsideCompilette):
fatalError ("Something weird append on line: "+line)
if outsideCompilette:
cCode += line
else:
compiletteCode += line
return prefixDict, cCode
def extractCompilette(fileIn):
""" Compilette code
"""
BEGIN = "#["
END = "]#"
outsideCompilette = True
for line in fileIn:
if (BEGIN in line and outsideCompilette):
outsideCompilette = False
line, compiletteCode = line.split(BEGIN)
elif END in line and not outsideCompilette:
outsideCompilette = True
endCompilette, line = line.split(END)
compiletteCode += endCompilette
elif (BEGIN in line and not outsideCompilette) or (END in line and outsideCompilette):
fatalError ("Something weird append on line: "+line)
if not outsideCompilette:
compiletteCode += line
return compiletteCode
if __name__ == '__main__':
import sys, os, argparse
archList = ("riscv", "power", "kalray", "cxram", "aarch64")
aliasDict = { "riscv": { "arch":["riscv", ],
"extension": [["RV32I", "RV32F", "RV32M", "RV32D"],],
"abi": "RV32G"},
"power": { "arch":["power",],
"extension": [["p1", "ppc", "v2.03", "v2.07", "v3.0", "3.0b"],],
"abi": "power" },
"kalray": { "arch":["kalray",],
"extension": [["kalray",], ],
"abi": "kalray" },
"gap9":{ "arch":["riscv", ],
"extension": [["RV32I", "RV32F", "RV32M", "RV32Xf16"], ],
"abi": "GAP9"},
"cxram":{ "arch":["riscv", "cxram"],
"extension": [["RV32I", "RV32F", "RV32M", "RV32D"], ["cxram"]],
"abi": "CXRAM"},
"aarch64":{ "arch":["aarch64"],
"extension": [["A64", "A32", "T32"], ],
"abi": "A64"},
}
parser = argparse.ArgumentParser("Hybrogen to C rewriter")
group = parser.add_mutually_exclusive_group(required=True)
parser.add_argument ('-i', '--inputfile', required=True, help="give input file name")
# parser.add_argument('fileName', type=str, help='file name')
parser.add_argument ('-a', '--arch', required=True, nargs='+', help="give arch parameter (archname & extension(s)) or alias : "+str(archList))
parser.add_argument ('-b', '--abi', help="give abi parameter")
# group.add_argument ('-t', '--tree', action='store_true', help="shows syntax tree")
group.add_argument ('-c', '--toC', action='store_true', help="rewrite to C")
group.add_argument ('-e', '--extract', action='store_true', help="extract compilettes source code")
parser.add_argument ('-v', '--verboseParsing', action='store_true', help="verbose parsing")
parser.add_argument ('-V', '--verboseBackend', action='store_true', help="verbose backend")
parser.add_argument ('-w', '--verboseIR', action='store_true', help="verbose IR")
parser.add_argument ('-g', '--debug', action='store_true', help="add function version instead of macros")
parser.add_argument ('-d', '--dbIds', default="localhost:hybrogen:hybrogen:hybrogen", help="give quadruplet database identification host:dbName:dbUser:dbpasswd")
args = parser.parse_args()
if args.arch[0] in aliasDict:
arch = aliasDict[args.arch[0]]["arch"]
extension = aliasDict[args.arch[0]]["extension"]
abi = aliasDict[args.arch[0]]["abi"]
elif (args.arch[0] not in archList):
fatalError ("Unsupported architecture:"+args.arch[0])
else:
arch = args.arch[0]
extension = args.arch[1:]
abi = args.abi
platform = {"arch": arch, "extension":extension, "abi":abi}
if args.extract:
fileIn = open(args.inputfile, 'r')
compiletteCode = extractCompilette(fileIn)
print(compiletteCode)
elif args.toC:
outFileName, ext = os.path.splitext(args.inputfile)
outFileName += ".c"
# Passe 1 construction de l'arbre
ids = args.dbIds.split(":")
dbIds = {"host": ids[0], "dbname": ids[1], "user": ids[2],"pwd": ids[3]}
print('HybroLang Compiler %s (host=%s dbname=%s user=%s)'%(H2_RELEASE, dbIds["host"], dbIds["dbname"], dbIds["user"]))
fileIn = open(args.inputfile, 'r')
prefixDict, cCode = writeCandParseCompilette(fileIn, platform, args.verboseParsing, args.debug, dbIds)
fileIn.close()
prefixTuples = list()
for arith in prefixDict.keys():
prefixTuples += [ (op, arith[0]) for op in prefixDict[arith]]
if args.verboseBackend:
print("PrefixTuples after compilation and optimization:")
print(prefixTuples)
# Connect to backend only for master architecture ?
gen = GenGeneratorFromDb (arch[0], extension[0], abi, dbIds, args.verboseIR, args.debug)
prefixCode = gen.genAndGetCode(prefixTuples)
fileOut = open(outFileName, 'w')
fileOut.write (prefixCode)
fileOut.write (cCode)
fileOut.close()
else:
fatalError ("action not recognized")