public static void PrintFunctionDeclaration(ADFunctionDeclaration fceDeclaration, string globalAssigns = "") { Console.WriteLine($"{fceDeclaration.Name}:"); Console.WriteLine("pushq %rbp"); Console.WriteLine("movq %rsp, %rbp"); int argumentsCounter = 1; foreach (var arg in fceDeclaration.Arguments) { argumentsCounter++; arg.STRecord.Address = $"{argumentsCounter * SIZE}(%rbp)"; } string arrayDeclarations = string.Empty; int localVariablesCounter = GetAllLocalVariablesDeclarations(fceDeclaration.Body, 0, ref arrayDeclarations); if (localVariablesCounter > 0) { Console.WriteLine($"subq ${localVariablesCounter * SIZE}, %rsp"); Console.WriteLine(arrayDeclarations); } var returnLabel = GenerateLabel(); if (fceDeclaration.Name.ToLower() == "main") { Console.WriteLine(globalAssigns); } foreach (IADNode node in fceDeclaration.Body) { PrintNode(node, "", "", returnLabel); } Console.WriteLine("movq $0, %rax"); Console.WriteLine($"{returnLabel}:"); Console.WriteLine($"movq %rbp, %rsp"); Console.WriteLine($"popq %rbp"); Console.WriteLine($"ret ${(argumentsCounter - 1) * SIZE}"); }
public static void PrintFunctionDeclaration(ADFunctionDeclaration declaration, string tab) { Console.WriteLine($"{tab}|- Function: {declaration.Name}"); if (declaration.Arguments.Count > 0) { Console.WriteLine($"{tab}\tArguments:"); foreach (var arg in declaration.Arguments) { Console.WriteLine($"{tab}\t\t\\-{arg.Name}"); } } if (declaration.Body.Count() > 0) { Console.WriteLine($"{tab}\tBody:"); foreach (var node in declaration.Body) { PrintNode(node, tab + "\t\t"); } } }
public static IADNode st_main() { var token = MainFSM.GetNextToken(); if (token.Type != TokenType.idType) { SyntaxError("Byl ocekavan identifikator funkce."); } if (STSearch(token.Attribute, true) != null) { SemanticError($"Funkce nebo globalni promenna se jmenem {token.Attribute} je j*z deklarovana."); } ADFunctionDeclaration fceDeclaration = new ADFunctionDeclaration() { Name = token.Attribute }; var newRecord = new STRecord() { Access = STAccess.global, Name = token.Attribute, Type = STType.function, Function = fceDeclaration }; FunctionsST.Records.Add(newRecord); if (MainFSM.GetNextToken().Type != TokenType.leftRBType) { SyntaxError("Byl ocekavan znak \'(\'"); } var arguments = decl_arg(); var symbolTable = new STable(); foreach (var arg in arguments) { var stRecord = new STRecord() { Access = STAccess.local, Name = arg, Type = STType.variable }; symbolTable.Records.Add(stRecord); fceDeclaration.Arguments.Add(new ADVariable { Name = arg, Type = ADVariable.VarType.variable, STRecord = stRecord }); } STablesStack.Push(symbolTable); if (MainFSM.GetNextToken().Type != TokenType.leftCBType) { SyntaxError("Byl ocekavan znak \'{\'"); } fceDeclaration.Body = statement_list(true); if (MainFSM.GetNextToken().Type != TokenType.rightCBType) { SyntaxError("Byl ocekavan znak \'}\'"); } STablesStack.Pop(); return(fceDeclaration); }
public static void prog(ADTree tree) { STablesStack.Push(new STable()); var printLongFunction = new ADFunctionDeclaration() { Name = "print_long" }; printLongFunction.Arguments.Add(new ADVariable()); var printNlFunction = new ADFunctionDeclaration() { Name = "print_nl" }; var printCharFunction = new ADFunctionDeclaration() { Name = "print_char" }; printCharFunction.Arguments.Add(new ADVariable()); FunctionsST.Records.Add(new STRecord() { Access = STAccess.global, Function = printLongFunction, Name = "print_long", Type = STType.function }); FunctionsST.Records.Add(new STRecord() { Access = STAccess.global, Function = printNlFunction, Name = "print_nl", Type = STType.function }); FunctionsST.Records.Add(new STRecord() { Access = STAccess.global, Function = printCharFunction, Name = "print_char", Type = STType.function }); if (MainFSM.PeekNextToken().Type == TokenType.longType) { var global_declarations = global_decl_list(); tree.Nodes.Add(global_declarations); } if (MainFSM.PeekNextToken().Type == TokenType.idType) { var statements_list = st_main_list(); foreach (var item in statements_list) { tree.Nodes.Add(item); } } if (MainFSM.PeekNextToken().Type == TokenType.eofType) { return; } else { SyntaxError("Byla ocekavana deklarace globalni promenne nebo funkce."); } }