Note that there are some explanatory texts on larger screens.

plurals
  1. PO*Value is not being generated into the LLVM code
    primarykey
    data
    text
    <p>I am attempting to write some compiler and use LLVM to generate intermediate code. Unfortunately, LLVM documentation is not very great and even somewhat confusing. </p> <p>At the moment I have lexer,grammar and AST implemented. I was also following some examples found on Internet. My current AST works as follows: it has the abstract base class Tree*, from which other trees inherit (so, like one for variable definition, one for statement list, one for binary expression etc.).</p> <p>I am trying to implement the variable definition, so for the input </p> <pre><code>class Test{ int main() { int x; } } </code></pre> <p>I want LLVM output to be:</p> <pre><code>; ModuleID = "Test" define i32 @main() { entry: %x = alloca i32 return i32 0 } </code></pre> <p>However, right now I can get %x = alloca i32 part to the part where main function is created, but the actual output is missing the %x = alloca i32. So, the output I'm getting is as follows:</p> <pre><code>; ModuleID = "Test" define i32 @main() { entry: return i32 0 } </code></pre> <p>my Codegen() for variable declaration is shown bellow (symbol table for now is just a list, I am trying to keep things as simple as possible at the moment):</p> <pre><code>llvm::Value *decafStmtList::Codegen() { string name = SyandTy.back(); // Just a name of a variable string type = SyandTy.front(); // and its type in string format Type* typeVal = getLLVMType(decafType(str2DecafType(type))); // get LLVM::*Type representation llvm::AllocaInst *Alloca = Builder.CreateAlloca(typeVal, 0, name.c_str()); Value *V = Alloca; return Alloca;//Builder.CreateLoad(V, name.c_str()); } </code></pre> <p>The part where I am generating my @main is as follows: Note: I have commented out the print_int function (this is the function I will use later to print things, but for now I don't need it). If I'll uncomment the print_int function, TheFunction will not pass verifier(TheFunction) -> complains about module being broken and parameters not matching the signature.</p> <pre><code>Function *gen_main_def(llvm::Value *RetVal, Function *print_int) { if (RetVal == 0) { throw runtime_error("something went horribly wrong\n"); } // create the top-level definition for main FunctionType *FT = FunctionType::get(IntegerType::get(getGlobalContext(), 32), false); Function *TheFunction = Function::Create(FT, Function::ExternalLinkage, "main", TheModule); if (TheFunction == 0) { throw runtime_error("empty function block"); } // Create a new basic block which contains a sequence of LLVM instructions BasicBlock *BB = BasicBlock::Create(getGlobalContext(), "entry", TheFunction); // All subsequent calls to IRBuilder will place instructions in this location Builder.SetInsertPoint(BB); /* Function *CalleeF = TheModule-&gt;getFunction(print_int-&gt;getName()); if (CalleeF == 0) { throw runtime_error("could not find the function print_int\n"); }*/ // print the value of the expression and we are done // Value *CallF = Builder.CreateCall(CalleeF, RetVal, "calltmp"); // Finish off the function. // return 0 from main, which is EXIT_SUCCESS Builder.CreateRet(ConstantInt::get(getGlobalContext(), APInt(32, 0))); return TheFunction; } </code></pre> <p>If someone knows why my Alloca object is not being generated, please help me out - any hints will be greatly appreciated.</p> <p>Thank you</p> <p>EDIT: </p> <p>Codegen is called from the grammar:</p> <pre><code>start: program program: extern_list decafclass { ProgramAST *prog = new ProgramAST((decafStmtList *)$1, (ClassAST *)$2); if (printAST) { cout &lt;&lt; getString(prog) &lt;&lt; endl; } Value *RetVal = prog-&gt;Codegen(); delete $1; // get rid of abstract syntax tree delete $2; // get rid of abstract syntax tree // we create an implicit print_int function call to print // out the value of the expression. Function *print_int = gen_print_int_def(); Function *TheFunction = gen_main_def(RetVal, print_int); verifyFunction(*TheFunction); } </code></pre> <p>EDIT: I figured it out, basically the createAlloca has to be called after the basicblock when generating main; </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload