-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* factor printf out of runtime * completely remove runtime * fix tester * add println * update readme
- Loading branch information
1 parent
c603d0a
commit 7e57934
Showing
21 changed files
with
206 additions
and
66 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,91 @@ | ||
#include "backend/builtin/print.h" | ||
#include "backend/io.h" | ||
#include "shared/context.h" | ||
|
||
void builtin::define_all() {} | ||
#include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
#include "mlir/Dialect/LLVMIR/LLVMTypes.h" | ||
#include "mlir/IR/Block.h" | ||
|
||
namespace { | ||
void create_type_str(TypePtr type) { | ||
std::string ty = type->get_specifier(); | ||
auto gvalue = mlir::StringRef(ty.c_str(), 3); | ||
|
||
auto gtype = | ||
mlir::LLVM::LLVMArrayType::get(ctx::ch->get_mlir(), gvalue.size()); | ||
|
||
ctx::builder->create<mlir::LLVM::GlobalOp>( | ||
*ctx::loc, gtype, true, mlir::LLVM::Linkage::Internal, type->get_name(), | ||
ctx::builder->getStringAttr(gvalue), 0); | ||
} | ||
|
||
void setupPrintf() { | ||
auto func_type = mlir::LLVM::LLVMFunctionType::get( | ||
ctx::i32->get_mlir(), ctx::ch->get_pointer(), true); | ||
|
||
ctx::builder->create<mlir::LLVM::LLVMFuncOp>(*ctx::loc, "printf", | ||
func_type); | ||
} | ||
|
||
void newline_type_str(TypePtr type) { | ||
std::string ty = type->get_specifier() + "\n"; | ||
auto gvalue = mlir::StringRef(ty.c_str(), 4); | ||
|
||
auto gtype = | ||
mlir::LLVM::LLVMArrayType::get(ctx::ch->get_mlir(), gvalue.size()); | ||
|
||
ctx::builder->create<mlir::LLVM::GlobalOp>( | ||
*ctx::loc, gtype, true, mlir::LLVM::Linkage::Internal, | ||
"newline_" + type->get_name(), ctx::builder->getStringAttr(gvalue), 0); | ||
} | ||
} // namespace | ||
|
||
void builtin::define_all() { | ||
setupPrintf(); | ||
|
||
create_type_str(ctx::i32); | ||
create_type_str(ctx::ch); | ||
|
||
newline_type_str(ctx::i32); | ||
newline_type_str(ctx::ch); | ||
|
||
define_print(ctx::i32); | ||
define_print(ctx::ch); | ||
|
||
define_println(ctx::i32); | ||
define_println(ctx::ch); | ||
} | ||
|
||
void builtin::define_print(TypePtr type) { | ||
auto func_type = mlir::LLVM::LLVMFunctionType::get( | ||
ctx::none->get_mlir(), {type->get_mlir()}, false); | ||
|
||
mlir::LLVM::LLVMFuncOp func = ctx::builder->create<mlir::LLVM::LLVMFuncOp>( | ||
*ctx::loc, "print_" + type->get_name(), func_type); | ||
|
||
mlir::Block* body = func.addEntryBlock(); | ||
ctx::builder->setInsertionPointToStart(body); | ||
|
||
mlir::Value arg = func.getArgument(0); | ||
io::printf(arg, type); | ||
|
||
ctx::builder->create<mlir::LLVM::ReturnOp>(*ctx::loc, mlir::Value{}); | ||
ctx::builder->setInsertionPointToEnd(ctx::module->getBody()); | ||
} | ||
|
||
void builtin::define_println(TypePtr type) { | ||
auto func_type = mlir::LLVM::LLVMFunctionType::get( | ||
ctx::none->get_mlir(), {type->get_mlir()}, false); | ||
|
||
mlir::LLVM::LLVMFuncOp func = ctx::builder->create<mlir::LLVM::LLVMFuncOp>( | ||
*ctx::loc, "println_" + type->get_name(), func_type); | ||
|
||
mlir::Block* body = func.addEntryBlock(); | ||
ctx::builder->setInsertionPointToStart(body); | ||
|
||
mlir::Value arg = func.getArgument(0); | ||
io::println(arg, type); | ||
|
||
ctx::builder->create<mlir::LLVM::ReturnOp>(*ctx::loc, mlir::Value{}); | ||
ctx::builder->setInsertionPointToEnd(ctx::module->getBody()); | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,38 @@ | ||
#include "backend/io.h" | ||
#include <string> | ||
#include "backend/types/integer.h" | ||
#include "backend/utils.h" | ||
#include "mlir/IR/BuiltinAttributes.h" | ||
#include "shared/context.h" | ||
|
||
#include "mlir/Dialect/LLVMIR/FunctionCallUtils.h" | ||
#include "mlir/Dialect/LLVMIR/LLVMDialect.h" | ||
#include "shared/context.h" | ||
|
||
void io::print(mlir::Value value, TypePtr type) { | ||
std::string name = "print_" + type->get_name(); | ||
mlir::LLVM::LLVMFuncOp func = mlir::LLVM::lookupOrCreateFn( | ||
*ctx::module, name, type->get_mlir(), ctx::none->get_mlir()); | ||
namespace { | ||
void print(mlir::Value value, TypePtr type, std::string name) { | ||
mlir::LLVM::GlobalOp global; | ||
if (!(global = ctx::module->lookupSymbol<mlir::LLVM::GlobalOp>(name))) { | ||
llvm::errs() << "missing format string!\n"; | ||
return; | ||
} | ||
|
||
mlir::Value global_ptr = | ||
ctx::builder->create<mlir::LLVM::AddressOfOp>(*ctx::loc, global); | ||
|
||
mlir::Value cst0 = integer::create_i32(0); | ||
mlir::Value arg = ctx::builder->create<mlir::LLVM::GEPOp>( | ||
*ctx::loc, ctx::ch->get_pointer(), global_ptr, | ||
mlir::ArrayRef<mlir::Value>({cst0, cst0})); | ||
|
||
mlir::LLVM::LLVMFuncOp func = | ||
ctx::module->lookupSymbol<mlir::LLVM::LLVMFuncOp>("printf"); | ||
utils::call(func, {arg, value}); | ||
} | ||
} // namespace | ||
|
||
void io::printf(mlir::Value value, TypePtr type) { | ||
print(value, type, type->get_name()); | ||
} | ||
|
||
ctx::builder->create<mlir::LLVM::CallOp>(*ctx::loc, func, value); | ||
void io::println(mlir::Value value, TypePtr type) { | ||
print(value, type, "newline_" + type->get_name()); | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,7 @@ fn main(): i32 { | |
print(x); | ||
print('\n'); | ||
|
||
println('z'); | ||
|
||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
fn main(): i32 { | ||
let x: i32 = 100; | ||
print(5); | ||
println(x); | ||
|
||
return 0; | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
90 | ||
8 | ||
5 | ||
1 | ||
6 | ||
2 | ||
0 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
a | ||
z |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
5 | ||
5100 |
Oops, something went wrong.