initializing compiler structure
							parent
							
								
									545a010e58
								
							
						
					
					
						commit
						392ce41e97
					
				
							
								
								
									
										57
									
								
								compiler.c
								
								
								
								
							
							
						
						
									
										57
									
								
								compiler.c
								
								
								
								
							|  | @ -1,11 +1,60 @@ | |||
| #include <stdio.h> | ||||
| 
 | ||||
| #include <llvm-c/Core.h> | ||||
| 
 | ||||
| #include "y.tab.h" | ||||
| #include "ast.h" | ||||
| #include "stack.h" | ||||
| 
 | ||||
| extern FILE *yyin; | ||||
| Stack *stack; | ||||
| Node *rootNode; | ||||
| 
 | ||||
| LLVMTypeRef WraithTypeToLLVMType(PrimitiveType type) | ||||
| { | ||||
|     switch (type) | ||||
|     { | ||||
|         case Int: | ||||
|             return LLVMInt64Type(); | ||||
| 
 | ||||
|         case UInt: | ||||
|             return LLVMInt64Type(); | ||||
|     } | ||||
| 
 | ||||
|     return NULL; | ||||
| } | ||||
| 
 | ||||
| void CompileFunction(LLVMModuleRef module, Node *functionDeclaration) | ||||
| { | ||||
|     uint32_t i; | ||||
|     Node *functionSignature = functionDeclaration->children[0]; | ||||
|     LLVMTypeRef paramTypes[functionSignature->children[2]->childCount]; | ||||
| 
 | ||||
|     for (i = 0; i < functionSignature->children[2]->childCount; i += 1) | ||||
|     { | ||||
|         LLVMTypeRef paramType = WraithTypeToLLVMType(functionSignature->children[0]->type); | ||||
|     } | ||||
| 
 | ||||
|     LLVMTypeRef returnType = WraithTypeToLLVMType(functionSignature->children[1]->type); | ||||
| 
 | ||||
|     LLVMAddFunction(module, functionSignature->children[0]->value.string, returnType); | ||||
| } | ||||
| 
 | ||||
| void Compile(LLVMModuleRef module, Node *node) | ||||
| { | ||||
|     uint32_t i; | ||||
| 
 | ||||
|     switch (node->syntaxKind) | ||||
|     { | ||||
|         case FunctionDeclaration: | ||||
|             CompileFunction(module, node); | ||||
|             break; | ||||
|     } | ||||
| 
 | ||||
|     for (i = 0; i < node->childCount; i += 1) | ||||
|     { | ||||
|         Compile(module, node->children[i]); | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| int main(int argc, char *argv[]) | ||||
| { | ||||
|  | @ -22,7 +71,11 @@ int main(int argc, char *argv[]) | |||
|     yyparse(fp, stack); | ||||
|     fclose(fp); | ||||
| 
 | ||||
|     LLVMModuleRef mod = LLVMModuleCreateWithName("my_module"); | ||||
|     PrintTree(rootNode, 0); | ||||
| 
 | ||||
|     LLVMModuleRef module = LLVMModuleCreateWithName("my_module"); | ||||
| 
 | ||||
|     Compile(module, rootNode); | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
|  |  | |||
							
								
								
									
										5
									
								
								wraith.y
								
								
								
								
							
							
						
						
									
										5
									
								
								wraith.y
								
								
								
								
							|  | @ -15,6 +15,8 @@ void yyerror(FILE *fp, Stack *stack, char *s) | |||
| extern char *yytext; | ||||
| extern int yylex (void); | ||||
| extern FILE *yyin; | ||||
| 
 | ||||
| extern Node *rootNode; | ||||
| %} | ||||
| 
 | ||||
| %define api.value.type {struct Node*} | ||||
|  | @ -73,7 +75,8 @@ Program                 : Declarations | |||
|                             declarationSequence = MakeDeclarationSequenceNode(declarations, declarationCount); | ||||
| 
 | ||||
|                             PopStackFrame(stack); | ||||
|                             PrintTree(declarationSequence, 0); | ||||
| 
 | ||||
|                             rootNode = declarationSequence; | ||||
|                         } | ||||
| 
 | ||||
| Type                    : INT | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue