Skip to content

Commit

Permalink
Print an error message if the runtime couldn't be started (#2431)
Browse files Browse the repository at this point in the history
This change is handled in generated code and doesn't affect the runtime
itself.
  • Loading branch information
Benoit Vey authored and jemc committed Dec 20, 2017
1 parent e96fa95 commit c2557a9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 14 deletions.
21 changes: 21 additions & 0 deletions src/libponyc/codegen/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,11 @@ static void init_runtime(compile_t* c)
param = LLVMGetParam(value, 1);
LLVMAddAttribute(param, LLVMReadOnlyAttribute);
#endif

// i32 puts(i8*)
params[0] = c->void_ptr;
type = LLVMFunctionType(c->i32, params, 1, false);
value = LLVMAddFunction(c->module, "puts", type);
}

static bool init_module(compile_t* c, ast_t* program, pass_opt_t* opt)
Expand Down Expand Up @@ -1422,6 +1427,22 @@ LLVMValueRef codegen_call(compile_t* c, LLVMValueRef fun, LLVMValueRef* args,
return result;
}

LLVMValueRef codegen_string(compile_t* c, const char* str, size_t len)
{
LLVMValueRef str_val = LLVMConstStringInContext(c->context, str, (int)len,
false);
LLVMValueRef g_str = LLVMAddGlobal(c->module, LLVMTypeOf(str_val), "");
LLVMSetLinkage(g_str, LLVMPrivateLinkage);
LLVMSetInitializer(g_str, str_val);
LLVMSetGlobalConstant(g_str, true);
LLVMSetUnnamedAddr(g_str, true);

LLVMValueRef args[2];
args[0] = LLVMConstInt(c->i32, 0, false);
args[1] = LLVMConstInt(c->i32, 0, false);
return LLVMConstInBoundsGEP(g_str, args, 2);
}

const char* suffix_filename(compile_t* c, const char* dir, const char* prefix,
const char* file, const char* extension)
{
Expand Down
2 changes: 2 additions & 0 deletions src/libponyc/codegen/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ LLVMBasicBlockRef codegen_block(compile_t* c, const char* name);
LLVMValueRef codegen_call(compile_t* c, LLVMValueRef fun, LLVMValueRef* args,
size_t count, bool setcc);

LLVMValueRef codegen_string(compile_t* c, const char* str, size_t len);

const char* suffix_filename(compile_t* c, const char* dir, const char* prefix,
const char* file, const char* extension);

Expand Down
18 changes: 18 additions & 0 deletions src/libponyc/codegen/genexe.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ LLVMValueRef gen_main(compile_t* c, reach_type_t* t_main, reach_type_t* t_env)

codegen_startfun(c, func, NULL, NULL, false);

LLVMBasicBlockRef start_fail_block = codegen_block(c, "start_fail");
LLVMBasicBlockRef post_block = codegen_block(c, "post");

LLVMValueRef args[5];
args[0] = LLVMGetParam(func, 0);
LLVMSetValueName(args[0], "argc");
Expand Down Expand Up @@ -129,6 +132,21 @@ LLVMValueRef gen_main(compile_t* c, reach_type_t* t_main, reach_type_t* t_env)
args[1] = LLVMConstInt(c->i32, 1, false);
LLVMValueRef rc = gencall_runtime(c, "pony_start", args, 2, "");

LLVMValueRef minus_one = LLVMConstInt(c->i32, (unsigned long long)-1, true);
LLVMValueRef start_success = LLVMBuildICmp(c->builder, LLVMIntNE, rc,
minus_one, "");
LLVMBuildCondBr(c->builder, start_success, post_block, start_fail_block);

LLVMPositionBuilderAtEnd(c->builder, start_fail_block);

const char error_msg_str[] = "Error: couldn't initialise runtime!";

args[0] = codegen_string(c, error_msg_str, sizeof(error_msg_str));
gencall_runtime(c, "puts", args, 1, "");
LLVMBuildBr(c->builder, post_block);

LLVMPositionBuilderAtEnd(c->builder, post_block);

// Run primitive finalisers. We create a new main actor as a context to run
// the finalisers in, but we do not initialise or schedule it.
if(c->primitives_final != NULL)
Expand Down
16 changes: 2 additions & 14 deletions src/libponyc/codegen/genreference.c
Original file line number Diff line number Diff line change
Expand Up @@ -507,26 +507,14 @@ LLVMValueRef gen_string(compile_t* c, ast_t* ast)

size_t len = ast_name_len(ast);

LLVMValueRef args[4];
args[0] = LLVMConstInt(c->i32, 0, false);
args[1] = LLVMConstInt(c->i32, 0, false);

LLVMValueRef str = LLVMConstStringInContext(c->context, name, (int)len,
false);
LLVMValueRef g_str = LLVMAddGlobal(c->module, LLVMTypeOf(str), "");
LLVMSetLinkage(g_str, LLVMPrivateLinkage);
LLVMSetInitializer(g_str, str);
LLVMSetGlobalConstant(g_str, true);
LLVMSetUnnamedAddr(g_str, true);
LLVMValueRef str_ptr = LLVMConstInBoundsGEP(g_str, args, 2);

reach_type_t* t = reach_type(c->reach, type);
compile_type_t* c_t = (compile_type_t*)t->c_type;

LLVMValueRef args[4];
args[0] = c_t->desc;
args[1] = LLVMConstInt(c->intptr, len, false);
args[2] = LLVMConstInt(c->intptr, len + 1, false);
args[3] = str_ptr;
args[3] = codegen_string(c, name, len);

LLVMValueRef inst = LLVMConstNamedStruct(c_t->structure, args, 4);
LLVMValueRef g_inst = LLVMAddGlobal(c->module, c_t->structure, "");
Expand Down

0 comments on commit c2557a9

Please sign in to comment.