/* Compile the AST into a module */ void CodeGenContext::generateCode(Block& root) { std::cout << "Generating code...\n"; std::cout << "Nodes: " << root.statements.size() << "\n"; /* Create the top level interpreter function to call as entry */ vector<const Type*> argTypes; FunctionType *ftype = FunctionType::get(Type::getVoidTy(getGlobalContext()), argTypes, false); mainFunction = Function::Create(ftype, GlobalValue::InternalLinkage, "main", module); BasicBlock *bblock = BasicBlock::Create(getGlobalContext(), "entry", mainFunction, 0); /* Push a new variable/block context */ pushBlock(bblock); root.codeGen(*this); /* emit bytecode for the toplevel block */ ReturnInst::Create(getGlobalContext(), bblock); popBlock(); /* Print the bytecode in a human-readable format to see if our program compiled properly */ std::cout << "Code is generated.\n"; PassManager pm; pm.add(createPrintModulePass(&outs())); pm.run(*module); }
/* Compile the AST into a module */ void CodeGenContext::generateCode(ast::Program& root) { std::cout << "Generating code...\n"; /* Create the top level interpreter function to call as entry */ std::vector<Type*> argTypes; FunctionType *ftype = FunctionType::get(Type::getVoidTy(getGlobalContext()), makeArrayRef(argTypes), false); // change GlobalValue::InternalLinkage into ExternalLinkage mainFunction = Function::Create(ftype, GlobalValue::ExternalLinkage, "main", module); BasicBlock *bblock = BasicBlock::Create(getGlobalContext(), "entry", mainFunction, 0); CodeGenContext::printf = createPrintf(*this); /* Push a new variable/block context */ pushBlock(bblock); currentFunction = mainFunction; for (auto label:labels){ labelBlock[label]=BasicBlock::Create(getGlobalContext(), "label", mainFunction, 0); } root.CodeGen(*this); /* emit bytecode for the toplevel block */ ReturnInst::Create(getGlobalContext(), currentBlock()); popBlock(); // popBlock(); /* Print the bytecode in a human-readable format to see if our program compiled properly */ std::cout << "Code is generated.\n"; PassManager pm; pm.add(createPrintModulePass(outs())); //pm.run(*module); // write IR to stderr std::cout<<"code is gen~~~\n"; module->dump(); std::cout<<"code is gen~!~\n"; }
int StatementBlock::execIntern(AbstractQoreNode** return_value, ExceptionSink* xsink) { QORE_TRACE("StatementBlock::execImpl()"); int rc = 0; assert(xsink); //printd(5, "StatementBlock::execImpl() this=%p, lvars=%p, %ld vars\n", this, lvars, lvars->size()); bool obe = !on_block_exit_list.empty(); // push "on block exit" iterator if necessary if (obe) pushBlock(on_block_exit_list.end()); // execute block for (statement_list_t::iterator i = statement_list.begin(), e = statement_list.end(); i != e; ++i) if ((rc = (*i)->exec(return_value, xsink)) || xsink->isEvent()) break; // execute "on block exit" code if applicable if (obe) { ExceptionSink obe_xsink; int nrc = 0; bool error = *xsink; for (block_list_t::iterator i = popBlock(), e = on_block_exit_list.end(); i != e; ++i) { enum obe_type_e type = (*i).first; if (type == OBE_Unconditional || (!error && type == OBE_Success) || (error && type == OBE_Error)) if ((*i).second) nrc = (*i).second->execImpl(return_value, &obe_xsink); } if (obe_xsink) xsink->assimilate(obe_xsink); if (nrc) rc = nrc; } return rc; }
//----------------------------------------------------------------- // Block hashing: From a file, calculate the MD5 hash value for // a block size of 512 or 4096 bytes in length //----------------------------------------------------------------- LPMD5BLOCK CalculateMD5Blocks(LPTSTR FileName, DWORD dwBlockSize) { HANDLE hashFile = NULL; BOOL bResult = FALSE; DWORD cbRead = 0; CHAR rgbDigits[] = "0123456789abcdef"; DWORD dwFileOffset = 0; BYTE* rgbFile; LPMD5BLOCK MD5Block; LPMD5BLOCK firstMD5Block; // Allocate space for the MD5BLOCK and actual block of data firstMD5Block = MYALLOC0(sizeof(LPMD5BLOCK)); rgbFile = MYALLOC0(dwBlockSize); // Open the file to perform hash hashFile = CreateFile(FileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL); // Check for an invalid file handle if (INVALID_HANDLE_VALUE == hashFile) { printf(">>> ERROR: CalculateMD5Blocks: ERROR_OPENING_FILE"); } // Read input file in 512 byte blocks; calculate MD5 and entropy for each block while (bResult = ReadFile(hashFile, rgbFile, dwBlockSize, &cbRead, NULL)) { INT *hist; INT histlen; DOUBLE H; LPTSTR lpszEntropy; // If number of read bytes is 0, break loop due to EOF if (0 == cbRead) { break; } // Calculate the MD5 hash value of block MD5Block = md5Block(rgbFile, dwFileOffset, cbRead); // Calculate the entropy value of block // First get the histogram hist = MYALLOC0(cbRead * sizeof(INT)); histlen = makehist(rgbFile, hist, cbRead); H = entropy(hist, histlen, cbRead); // Second, calculate the actual block entropy lpszEntropy = MYALLOC0(10 * sizeof(TCHAR)); swprintf_s(lpszEntropy, 10, L"%f", H); // Add entropy value to block structure MD5Block->fEntropy = H; // Check block structure was created if (MD5Block == NULL) { printf(">>> ERROR: CalculateMD5Blocks: Error creating MD5BLOCK"); break; } // Add block to FILECONTENT if (dwFileOffset == 0) { firstMD5Block = MD5Block; } else { pushBlock(firstMD5Block, MD5Block); } // Increase the file offset based on number of bytes read dwFileOffset += cbRead; } // Close the file handle CloseHandle(hashFile); // Return the first block (linked to all other blocks) return firstMD5Block; }
void genbf_selection_statement(struct selection_statement *a) { char *nname, *pblockname; int pblocknum; switch (a->type) { case _IF: case _IF_ELSE: genbf_expr(a->v1, 0, NULL); /* this will use a sneaky "subblock" format to make the jump-back * location predictable. basically: * main: * if (blah) { * main!0!1 * } else { * main!1!1 * } * main!2 */ /* get an "if-not" as well */ pblockname = curblock->name; pblocknum = curblock->num; printf("[>>>+>+<<<<-]>>>>[<<<<+>>>>-]+" "<[[-]>-<<<<(%s!%d)>>>]" ">[-<<<<(%s!%d)>>>>]" "<<<<", pblockname, pblocknum + 1, pblockname, pblocknum + 2); popVar(); /* go on to the if-block */ pushSubBlock(0); outBlock(); genbf_statement(a->v2); /* this needs to continue to the proper place */ if (a->type == _IF) { printf("(%s!%d)", pblockname, pblocknum + 2); } else { printf("(%s!%d)", pblockname, pblocknum + 3); /* this is an if/else, so now we need yet another subblock */ popNamedBlock(); pushSubBlock(1); outBlock(); genbf_statement(a->v3); printf("(%s!%d)", pblockname, pblocknum + 3); } /* finally continue with our regularly scheduled programming */ popNamedBlock(); pushBlock(); if (a->type == _IF) { curblock->num += 1; } else { curblock->num += 2; } outBlock(); break; case _SWITCH: UNIMPL("selection_statement"); /* SPC; printf("switch (\n"); genbf_expr(a->v1); SPC; printf(")\n"); genbf_statement(a->v2); */ break; } }