def visitFunction_definition( self, ctx: GrammarParser.Function_definitionContext): tyype = ctx.tyype().getText() name = ctx.identifier().getText() self.filee = open("out.ll", "a") out = "define " + llvm_type(tyype) + " @" + name + "(" self.filee.write(out) params = self.visit(ctx.arguments()) if len(params) > 0: out = llvm_type(params[0]) + " " + "%0" self.filee.write(out) for i in range(1, len(params)): out = ", " + llvm_type(params[i]) + " %" + str(i) self.filee.write(out) self.filee.write(") {\n") if tyype == Type.VOID: out = " ret " + llvm_type(tyype) + "\n" self.filee.write(out) cte_value = None ir_register = None self.ids_defined[name] = tyype, params, cte_value, ir_register self.inside_what_function = name self.next_ir_register = len(params) + 1 self.visit(ctx.body()) self.filee.write("}\n\n") self.next_ir_register = 0 self.filee.close() return
def visitFunction_definition( self, ctx: GrammarParser.Function_definitionContext): tyype = ctx.tyype().getText() name = ctx.identifier().getText() params = self.visit(ctx.arguments()) cte_value = None ir_register = [] f.write("define " + llvm_type(tyype) + " @" + name + "(") for idx, param in enumerate(params): if idx >= 1: f.write(", ") f.write(llvm_type(param[0]) + " %" + str(idx)) f.write(") {\n") for idx, alloc_param in enumerate(params): f.write("\t%" + alloc_param[1] + " = alloca " + llvm_type(alloc_param[0]) + ", align 4\n") f.write("\tstore " + llvm_type(alloc_param[0]) + " %" + str(idx) + ", " + llvm_type(alloc_param[0]) + "* " + "%" + alloc_param[1] + ", align 4\n") self.ids_defined[ alloc_param[1]] = alloc_param[0], [], None, alloc_param[1] self.ids_defined[name] = tyype, params, cte_value, ir_register self.inside_what_function = name self.next_ir_register = len(params) + 1 self.visit(ctx.body()) f.write("}\n\n") return
def visitFunction_definition(self, ctx:GrammarParser.Function_definitionContext): tyype = ctx.tyype().getText() name = ctx.identifier().getText() params = self.visit(ctx.arguments()) cte_value = None ir_register = None self.ids_defined[name] = tyype, params, cte_value, ir_register self.func_count[name] = 0 self.inside_what_function = name self.next_ir_register = len(params) + 1 #print function definiton print('define ' + str(llvm_type(tyype)) + ' @'+name +' (', end="") for i, p in enumerate(params): if(i < len(params)-1): print( str(llvm_type(p[0]))+' %'+ str(p[2])+',', end="") else: print( str(llvm_type(p[0]))+' %'+ str(p[2]), end="") self.func_count[name] = i+1 print(') {') self.print_arg(params) self.visit(ctx.body()) print('}') return
def visitFunction_definition( self, ctx: GrammarParser.Function_definitionContext): tyype = ctx.tyype().getText() name = ctx.identifier().getText() params = self.visit(ctx.arguments()) self.ids_defined[name] = tyype, params self.inside_what_function = name self.visit(ctx.body()) return
def visitFunction_definition(self, ctx:GrammarParser.Function_definitionContext): if ctx.identifier() != None: text = ctx.identifier().getText() function_type = ctx.tyype().getText() token = ctx.identifier().IDENTIFIER().getPayload() if (text in GrammarCheckerVisitor.ids_defined) == False: GrammarCheckerVisitor.ids_defined[text] = [function_type, len(ctx.arguments().identifier())] else: raise NameError("function definition '" + text + "'() linha: "+ str(token.line) + " ,já foi definido anteriormente.") for i in range(len(ctx.arguments().identifier())): text1 = ctx.arguments().identifier(i).getText() tyype = ctx.arguments().tyype(i).getText() token1 = ctx.arguments().identifier(i).IDENTIFIER().getPayload() if (text1 in GrammarCheckerVisitor.ids_defined) == False: GrammarCheckerVisitor.ids_defined[text1] = tyype GrammarCheckerVisitor.ids_defined[text].append(text1) else: raise NameError("identifier'" + text + "' linha: "+ str(token1.line) + " ,já foi definido anteriormente.") key = 0 for i in range(len(ctx.body().statement())): if 'return' == ctx.body().statement(i).children[0].getText(): key = 1 if function_type == Type.VOID: raise NameError("function definition '" + text + "'() linha: "+ str(token.line) + " ,não possui retorno.") else: expr_type = self.visit(ctx.body().statement(i).expression()) if expr_type != function_type: if (function_type == Type.INT and expr_type == Type.FLOAT) or (function_type == Type.FLOAT and expr_type == Type.INT) : print("WARNING: function definition '" + text + "'() linha: "+ str(token.line) + " ,pode perder informações, por retorna um tipo diferente da definição da função") else: raise NameError("function definition '" + text + "'() linha: "+ str(token.line) + " ,retorna um tipo diferente da definição da função.") else: self.visit(ctx.body().statement(i)) if key == 0 and function_type != Type.VOID: raise NameError("function definition '" + text + "'() linha: "+ str(token.line) + " ,não está retornando nada.")
def visitFunction_definition(self, ctx:GrammarParser.Function_definitionContext): tyype = ctx.tyype().getText() # mostra o tipo da funcao name = ctx.identifier().getText() #mostra o nome da funcao params = self.visit(ctx.arguments()) #pega os tipos do parametros e coloca na tabela de simbolos primeiro depois pega o tipo e nome da funcao self.ids_defined[name] = tyype, params, None #print(self.ids_defined) self.inside_what_function = name self.visit(ctx.body()) #print(ctx.body()) return
def visitFunction_definition( self, ctx: GrammarParser.Function_definitionContext): tyype = ctx.tyype().getText() name = ctx.identifier().getText() params = self.visit(ctx.arguments()) cte_value = None ir_register = None self.ids_defined[name] = tyype, params, cte_value, ir_register self.inside_what_function = name self.next_ir_register = len(params) + 1 self.visit(ctx.body()) return
def visitFunction_definition( self, ctx: GrammarParser.Function_definitionContext): arquivo.write("\n") self.next_ir_register = 0 tyype = ctx.tyype().getText() name = ctx.identifier().getText() params = self.visit(ctx.arguments()) cte_value = None ir_register = None params_tyypes = [self.ids_defined[name][0] for name in params] self.ids_defined[name] = tyype, params_tyypes, cte_value, ir_register self.inside_what_function = name self.next_ir_register = len(params) + 1 params_text = "" for index in range(len(params_tyypes)): if index > 0: params_text += ", " params_text += "{} %{}".format(llvm_type(params_tyypes[index]), index) arquivo.write( "define {} @{}({})".format(llvm_type(tyype), name, params_text) + " {\n") for index in range(len(params)): printAlloca(params[index], llvm_type(params_tyypes[index]), 4, True) printStore(index, llvm_type(params_tyypes[index]), params[index], llvm_type(params_tyypes[index]), 4, True) if len(params) != 0: arquivo.write("\n") self.visit(ctx.body()) arquivo.write("}\n") return
def visitFunction_definition( self, ctx: GrammarParser.Function_definitionContext): tyype = ctx.tyype().getText() name = ctx.identifier().getText() printf("define %s @%s(", llvm_type(tyype), name) params = self.visit(ctx.arguments()) if len(params) > 0: printf("%s %%0", llvm_type(params[0])) for i in range(1, len(params)): printf(", %s %%%d", llvm_type(params[i]), i) printf(") {\n") cte_value = None ir_register = None self.ids_defined[name] = tyype, params, cte_value, ir_register self.inside_what_function = name self.next_ir_register = len(params) + 1 self.visit(ctx.body()) if llvm_type(tyype) == "void": printf(" ret void\n") printf("}\n\n") return