Skip to content

Commit

Permalink
[cpp] codegen - __builtin_debugbreak now emits int3 inline asm as opp…
Browse files Browse the repository at this point in the history
…osed to an llvm trap instruction. this is because the trap instruction is not continuable, making it a very shitty debugbreak. stdlib.psy adds a new function assert(bool, msg) which does exactly wha tyou think it does
  • Loading branch information
harrand committed May 25, 2024
1 parent 585e571 commit c10a1c2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
10 changes: 9 additions & 1 deletion cpp/src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "builtin.hpp"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/IR/DIBuilder.h"
#include "llvm/IR/InlineAsm.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"

Expand Down Expand Up @@ -1814,8 +1815,15 @@ namespace code
}
break;
case builtin::debugbreak:
ret.llv = builder->CreateCall(llvm::Intrinsic::getDeclaration(program.get(), llvm::Intrinsic::trap));
{
// trap doesn't seem to be continuable so this is not a good fit.
//ret.llv = builder->CreateCall(llvm::Intrinsic::getDeclaration(program.get(), llvm::Intrinsic::trap));
// inline-asm int3 is a winner if it can be done:
llvm::InlineAsm* int3 = llvm::InlineAsm::get(llvm::FunctionType::get(llvm::Type::getVoidTy(*ctx), false), "int3", "~{dirflag},~{fpsr},~{flags}", true, false, llvm::InlineAsm::AsmDialect::AD_ATT);
llvm::CallInst* int3call = builder->CreateCall(int3, {});
int3call->addAttributeAtIndex(llvm::AttributeList::FunctionIndex, llvm::Attribute::NoUnwind);
ret.ty = func.return_ty;
}
break;
default:
d.ctx.error(error_code::nyi, "missing codegen for builtin \"{}\"", call.function_name);
Expand Down
1 change: 1 addition & 0 deletions cpp/src/parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,7 @@ namespace parse
shift();
while(this->reduce()){}
}
while(this->reduce()){}
// next level and go again (until when???)
std::size_t error_count = 0;

Expand Down
1 change: 1 addition & 0 deletions samples/scratchpad.psy
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
main :: () -> i64
{
puts("hello world! ;)");
assert(false, "woopsie!");
greeting : string;
defer greeting.cleanup();

Expand Down
18 changes: 14 additions & 4 deletions stdlib/stdlib.psy
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,18 @@ string :: struct
strcpy(this.str, literal);
}

//print :: () -> u0
//{
// puts(this.str);
//}
print :: () -> u0
{
puts(this.str);
}
}

assert :: (expr : bool, msg : i8& const) -> u0
{
if (expr != true)
{
puts(msg);
__builtin_debugbreak();
}
}
//

0 comments on commit c10a1c2

Please sign in to comment.