预定义了getint,putint两个库函数,可以与运行时库链接

main
wqz 3 years ago
parent 53dcd83a00
commit a20e52b95c

@ -18,14 +18,18 @@ namespace sysy
auto pModule = new Module();
assert(pModule);
module.reset(pModule);
// // create function:getint
// auto getint_type = Type::getFunctionType(Type::getIntType());
// auto f_getint = pModule->createFunction("getint", getint_type);
// symbols.insert("getint", f_getint);
// // create function:putint
// auto putint_type = Type::getFunctionType(Type::getVoidType(), vector<Type *>({Type::getIntType()}));
// auto f_putint = pModule->createFunction("putint", putint_type);
// symbols.insert("putint", f_putint);
// create function:getint
auto getint_type = Type::getFunctionType(Type::getIntType());
auto f_getint = pModule->createFunction("getint", getint_type);
symbols.insert("getint", f_getint);
auto getint_entry = f_getint->addBasicBlock("getint_entry");
// create function:putint
auto putint_type = Type::getFunctionType(Type::getVoidType(), vector<Type *>({Type::getIntType()}));
auto f_putint = pModule->createFunction("putint", putint_type);
symbols.insert("putint", f_putint);
// SymbolTable::FunctionScope putint_scope(symbols);
auto putint_entry = f_putint->addBasicBlock("putint_entry");
auto putint_arg = putint_entry->createArgument(Type::getPointerType(Type::getIntType()));
// generates globals and functions
visitChildren(ctx);
// return the IR module
@ -116,7 +120,7 @@ namespace sysy
symbols.insert(name, function);
// create the function scope
SymbolTable::FunctionScope scope(symbols);
// create the entry block with the same parameters as the function,
// create the entry block with the same parameters of the function,
// and update the symbol table
auto entry = function->addBasicBlock(entry_name);
// auto entry = function->getEntryBlock();

@ -183,6 +183,9 @@ namespace backend
string CodeGen::function_gen(Function *func)
{
string code;
if (libfunc.find(func->getName()) != libfunc.end())
return code;
curFunc = func;
clearFunctionRecord(func);
string bbCode;
@ -196,7 +199,6 @@ namespace backend
auto bb = iter->get();
bbCode += basicBlock_gen(bb);
}
string code;
string funcHead = functionHead_gen(func);
string prologueCode = prologueCode_gen(func);
string epilogueCode = epilogueCode_gen(func);

@ -188,6 +188,7 @@ namespace backend
int max_param = 0;
// record arguments need to be backpatched
vector<Argument *> backpatch;
set<string> libfunc = {"getint", "putint"};
public:
CodeGen(Module *module) : module(module) {}

Loading…
Cancel
Save