Last active
August 29, 2015 14:23
-
-
Save vasek/1374acbd0674bfc3b2b6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 0. // Vyhledáš funkci v tabulce symbolů a pokud už existuje, vyhodíš chybu | |
| 1. Parametry: | |
| // Každý parametrů zvlášť přeložíš, vytvoříš pro něj deklaraci PARM_DECL a přiřadíš typ. | |
| paramTree = param->Translate(NULL_TREE, symbTable); | |
| paramDeclTree = build_decl(UNKNOWN_LOCATION, PARM_DECL, get_identifier(param->name), paramTree); | |
| DECL_ARG_TYPE(paramDeclTree) = paramTree; | |
| // Pak přidáš do seznamu parametrů a seznamu deklarací parametrů (defaulně obě NULL_TREE). | |
| paramsTree = chainon(paramsTree, tree_cons(NULL_TREE, paramTree, NULL_TREE)); | |
| paramsDeclTree = chainon(paramsDeclTree, paramDeclTree); | |
| 2. Návratová hodnota funkce: | |
| if (isProcedure()) | |
| resultTree = void_type_node; | |
| else | |
| resultTree = resultType->Translate(NULL_TREE, symbTable); | |
| resultDeclTree = build_decl(BUILTINS_LOCATION, RESULT_DECL, NULL_TREE, resultTree); | |
| functionTree = build_function_type(TREE_TYPE(resultDeclTree), paramsTree); | |
| 3. Funkce: | |
| // Vytvoříš deklaraci a přiřadíš k ní argumenty a návratový typ. | |
| functionDeclTree = build_decl(UNKNOWN_LOCATION, FUNCTION_DECL, get_identifier(function->name), functionTree); | |
| DECL_ARGUMENTS(functionDeclTree) = paramsDeclTree; | |
| DECL_RESULT(functionDeclTree) = resultDeclTree; | |
| TREE_STATIC(functionDeclTree) = true; | |
| TREE_PUBLIC(functionDeclTree) = true; | |
| // (Přidáš funkci do tabulky symbolů) | |
| 4. Tělo funkce: | |
| // Vytvořím lokální kopii tabulky symbolů, do které přidám argumenty jako klasické proměnné | |
| SymbTable localSymbTable(symbTable); | |
| // Naalokuju seznam příkazů, do kterého přidám deklarace lokálních proměnných a pak jednotlivé příkazy | |
| statementsTree = alloc_stmt_list(); | |
| declarationTree = declaration->Translate(functionDeclTree, localSymbTable); | |
| declarationsTree = chainon(declarationsTree, declarationTree); | |
| append_to_statement_list(build1(DECL_EXPR, void_type_node, declaration), &statementsTree); | |
| // Vytvořím blok | |
| blockTree = build_block(declarationsTree, NULL_TREE, NULL_TREE, NULL_TREE); | |
| TREE_USED(blockTree) = true; | |
| // Přidám jednotlivé příkazy | |
| statementTree = statement->Translate(block, localSymbTable); | |
| append_to_statement_list(statementTree, &statementsTree); | |
| // Pokud jde o funkci a ne o proceduru, přidám na konec ještě explicitní příkaz return. | |
| returnStatementTree = build1(RETURN_EXPR, TREE_TYPE(resultDeclTree), resultDeclTree); | |
| append_to_statement_list(resultStatementTree, &statementsTree); | |
| // Přiřadím tělo k funkci | |
| bindTree = build3(BIND_EXPR, void_type_node, BLOCK_VARS(blockTree), NULL_TREE, blockTree); | |
| BIND_EXPR_BODY(bindTree) = statementsTree; | |
| TREE_SIDE_EFFECTS(bindTree) = true; | |
| BLOCK_SUPERCONTEXT(blockTree) = functionDeclTree; | |
| DECL_INITIAL(functionDeclTree) = blockTree; | |
| DECL_SAVED_TREE(functionDeclTree) = bindTree; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment