Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use deferred reification during code generation #2557

Merged
merged 1 commit into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/libponyc/codegen/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -977,12 +977,13 @@ LLVMValueRef codegen_addfun(compile_t* c, const char* name, LLVMTypeRef type,
}

void codegen_startfun(compile_t* c, LLVMValueRef fun, LLVMMetadataRef file,
LLVMMetadataRef scope, bool bare)
LLVMMetadataRef scope, deferred_reification_t* reify, bool bare)
{
compile_frame_t* frame = push_frame(c);

frame->fun = fun;
frame->is_function = true;
frame->reify = reify;
frame->bare_function = bare;
frame->di_file = file;
frame->di_scope = scope;
Expand All @@ -1006,6 +1007,7 @@ void codegen_pushscope(compile_t* c, ast_t* ast)
compile_frame_t* frame = push_frame(c);

frame->fun = frame->prev->fun;
frame->reify = frame->prev->reify;
frame->bare_function = frame->prev->bare_function;
frame->break_target = frame->prev->break_target;
frame->break_novalue_target = frame->prev->break_novalue_target;
Expand Down Expand Up @@ -1111,6 +1113,7 @@ void codegen_pushloop(compile_t* c, LLVMBasicBlockRef continue_target,
compile_frame_t* frame = push_frame(c);

frame->fun = frame->prev->fun;
frame->reify = frame->prev->reify;
frame->bare_function = frame->prev->bare_function;
frame->break_target = break_target;
frame->break_novalue_target = break_novalue_target;
Expand All @@ -1130,6 +1133,7 @@ void codegen_pushtry(compile_t* c, LLVMBasicBlockRef invoke_target)
compile_frame_t* frame = push_frame(c);

frame->fun = frame->prev->fun;
frame->reify = frame->prev->reify;
frame->bare_function = frame->prev->bare_function;
frame->break_target = frame->prev->break_target;
frame->break_novalue_target = frame->prev->break_novalue_target;
Expand Down
3 changes: 2 additions & 1 deletion src/libponyc/codegen/codegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ typedef struct compile_frame_t
bool is_function;
bool early_termination;
bool bare_function;
deferred_reification_t* reify;

struct compile_frame_t* prev;
} compile_frame_t;
Expand Down Expand Up @@ -240,7 +241,7 @@ LLVMValueRef codegen_addfun(compile_t* c, const char* name, LLVMTypeRef type,
bool pony_abi);

void codegen_startfun(compile_t* c, LLVMValueRef fun, LLVMMetadataRef file,
LLVMMetadataRef scope, bool bare);
LLVMMetadataRef scope, deferred_reification_t* reify, bool bare);

void codegen_finishfun(compile_t* c);

Expand Down
87 changes: 58 additions & 29 deletions src/libponyc/codegen/gencall.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,17 +214,23 @@ static bool special_case_call(compile_t* c, ast_t* ast, LLVMValueRef* value)
return false;

AST_GET_CHILDREN(postfix, receiver, method);
ast_t* receiver_type = ast_type(receiver);
ast_t* receiver_type = deferred_reify(c->frame->reify, ast_type(receiver),
c->opt);

if(ast_id(receiver_type) != TK_NOMINAL)
return false;
const char* name = NULL;

AST_GET_CHILDREN(receiver_type, package, id);
if(ast_id(receiver_type) == TK_NOMINAL)
{
AST_GET_CHILDREN(receiver_type, package, id);

if(ast_name(package) != c->str_builtin)
return false;
if(ast_name(package) == c->str_builtin)
name = ast_name(id);
}

ast_free_unattached(receiver_type);

const char* name = ast_name(id);
if(name == NULL)
return false;

if(name == c->str_Bool)
return special_case_operator(c, ast, value, true, true);
Expand Down Expand Up @@ -478,7 +484,7 @@ LLVMValueRef gen_funptr(compile_t* c, ast_t* ast)
LLVMValueRef value = gen_expr(c, receiver);

// Get the receiver type.
ast_t* type = ast_type(receiver);
ast_t* type = deferred_reify(c->frame->reify, ast_type(receiver), c->opt);
reach_type_t* t = reach_type(c->reach, type);
pony_assert(t != NULL);

Expand All @@ -487,6 +493,8 @@ LLVMValueRef gen_funptr(compile_t* c, ast_t* ast)
reach_method_t* m = reach_method(t, cap, name, typeargs);
LLVMValueRef funptr = dispatch_function(c, t, m, value);

ast_free_unattached(type);

if((m->cap != TK_AT) && (c->linkage != LLVMExternalLinkage))
{
// We must reset the function linkage and calling convention since we're
Expand Down Expand Up @@ -535,13 +543,18 @@ void gen_send_message(compile_t* c, reach_method_t* m, LLVMValueRef args[],
size_t args_buf_size = (m->param_count + 1) * sizeof(LLVMValueRef);
LLVMValueRef* cast_args =
(LLVMValueRef*)ponyint_pool_alloc_size(args_buf_size);
size_t arg_types_buf_size = m->param_count * sizeof(ast_t*);
ast_t** arg_types = (ast_t**)ponyint_pool_alloc_size(arg_types_buf_size);

ast_t* arg_ast = ast_child(args_ast);

deferred_reification_t* reify = c->frame->reify;

for(size_t i = 0; i < m->param_count; i++)
{
arg_types[i] = deferred_reify(reify, ast_type(arg_ast), c->opt);
cast_args[i+1] = gen_assign_cast(c, param_types[i+3], args[i+1],
ast_type(arg_ast));
arg_types[i]);
arg_ast = ast_sibling(arg_ast);
}

Expand All @@ -561,18 +574,15 @@ void gen_send_message(compile_t* c, reach_method_t* m, LLVMValueRef args[],
}

// Trace while populating the message contents.
arg_ast = ast_child(args_ast);
bool need_trace = false;

for(size_t i = 0; i < m->param_count; i++)
{
if(gentrace_needed(c, ast_type(arg_ast), m->params[i].ast))
if(gentrace_needed(c, arg_types[i], m->params[i].ast))
{
need_trace = true;
break;
}

arg_ast = ast_sibling(arg_ast);
}

LLVMValueRef ctx = codegen_ctx(c);
Expand All @@ -581,13 +591,11 @@ void gen_send_message(compile_t* c, reach_method_t* m, LLVMValueRef args[],
{
LLVMValueRef gc = gencall_runtime(c, "pony_gc_send", &ctx, 1, "");
LLVMSetMetadataStr(gc, "pony.msgsend", md);
arg_ast = ast_child(args_ast);

for(size_t i = 0; i < m->param_count; i++)
{
gentrace(c, ctx, args[i+1], cast_args[i+1], ast_type(arg_ast),
gentrace(c, ctx, args[i+1], cast_args[i+1], arg_types[i],
m->params[i].ast);
arg_ast = ast_sibling(arg_ast);
}

gc = gencall_runtime(c, "pony_send_done", &ctx, 1, "");
Expand All @@ -611,6 +619,11 @@ void gen_send_message(compile_t* c, reach_method_t* m, LLVMValueRef args[],

ponyint_pool_free_size(params_buf_size, param_types);
ponyint_pool_free_size(args_buf_size, cast_args);

for(size_t i = 0; i < m->param_count; i++)
ast_free_unattached(arg_types[i]);

ponyint_pool_free_size(arg_types_buf_size, arg_types);
}

static bool contains_boxable(ast_t* type)
Expand Down Expand Up @@ -726,6 +739,8 @@ LLVMValueRef gen_call(compile_t* c, ast_t* ast)
AST_GET_CHILDREN(postfix, receiver, method);
ast_t* typeargs = NULL;

deferred_reification_t* reify = c->frame->reify;

// Dig through function qualification.
switch(ast_id(receiver))
{
Expand All @@ -735,7 +750,7 @@ LLVMValueRef gen_call(compile_t* c, ast_t* ast)
case TK_FUNREF:
case TK_BECHAIN:
case TK_FUNCHAIN:
typeargs = method;
typeargs = deferred_reify(reify, method, c->opt);
AST_GET_CHILDREN_NO_DECL(receiver, receiver, method);
break;

Expand All @@ -744,12 +759,18 @@ LLVMValueRef gen_call(compile_t* c, ast_t* ast)

// Get the receiver type.
const char* method_name = ast_name(method);
ast_t* type = ast_type(receiver);
ast_t* type = deferred_reify(reify, ast_type(receiver), c->opt);
reach_type_t* t = reach_type(c->reach, type);
pony_assert(t != NULL);

token_id cap = cap_dispatch(type);
reach_method_t* m = reach_method(t, cap, method_name, typeargs);

ast_free_unattached(type);
ast_free_unattached(typeargs);

// Generate the arguments.
size_t count = ast_childcount(positional) + 1;
size_t count = m->param_count + 1;
size_t buf_size = count * sizeof(void*);

LLVMValueRef* args = (LLVMValueRef*)ponyint_pool_alloc_size(buf_size);
Expand Down Expand Up @@ -802,8 +823,6 @@ LLVMValueRef gen_call(compile_t* c, ast_t* ast)
}

// Static or virtual dispatch.
token_id cap = cap_dispatch(type);
reach_method_t* m = reach_method(t, cap, method_name, typeargs);
LLVMValueRef func = dispatch_function(c, t, m, args[0]);

bool is_message = false;
Expand Down Expand Up @@ -836,9 +855,6 @@ LLVMValueRef gen_call(compile_t* c, ast_t* ast)
{
// If we're sending a message, trace and send here instead of calling the
// sender to trace the most specific types possible.
token_id cap = cap_dispatch(type);
reach_method_t* m = reach_method(t, cap, method_name, typeargs);

codegen_debugloc(c, ast);
gen_send_message(c, m, args, positional);
codegen_debugloc(c, NULL);
Expand All @@ -863,7 +879,9 @@ LLVMValueRef gen_call(compile_t* c, ast_t* ast)

while(arg != NULL)
{
args[i] = gen_assign_cast(c, params[i], args[i], ast_type(arg));
ast_t* arg_type = deferred_reify(reify, ast_type(arg), c->opt);
args[i] = gen_assign_cast(c, params[i], args[i], arg_type);
ast_free_unattached(arg_type);
arg = ast_sibling(arg);
i++;
}
Expand Down Expand Up @@ -918,7 +936,8 @@ LLVMValueRef gen_call(compile_t* c, ast_t* ast)
LLVMValueRef gen_pattern_eq(compile_t* c, ast_t* pattern, LLVMValueRef r_value)
{
// This is used for structural equality in pattern matching.
ast_t* pattern_type = ast_type(pattern);
ast_t* pattern_type = deferred_reify(c->frame->reify, ast_type(pattern),
c->opt);

if(ast_id(pattern_type) == TK_NOMINAL)
{
Expand Down Expand Up @@ -948,6 +967,7 @@ LLVMValueRef gen_pattern_eq(compile_t* c, ast_t* pattern, LLVMValueRef r_value)
(name == c->str_F64)
)
{
ast_free_unattached(pattern_type);
return gen_eq_rvalue(c, pattern, r_value, true);
}
}
Expand All @@ -963,6 +983,8 @@ LLVMValueRef gen_pattern_eq(compile_t* c, ast_t* pattern, LLVMValueRef r_value)
reach_method_t* m = reach_method(t, cap, c->str_eq, NULL);
LLVMValueRef func = dispatch_function(c, t, m, l_value);

ast_free_unattached(pattern_type);

if(func == NULL)
return NULL;

Expand Down Expand Up @@ -1042,16 +1064,20 @@ static LLVMValueRef declare_ffi(compile_t* c, const char* f_name,

ast_t* arg = ast_child(args);

deferred_reification_t* reify = c->frame->reify;

while(arg != NULL)
{
ast_t* p_type = ast_type(arg);

if(p_type == NULL)
p_type = ast_childidx(arg, 1);

p_type = deferred_reify(reify, p_type, c->opt);
reach_type_t* pt = reach_type(c->reach, p_type);
pony_assert(pt != NULL);
f_params[count++] = ((compile_type_t*)pt->c_type)->use_type;
ast_free_unattached(p_type);
arg = ast_sibling(arg);
}

Expand Down Expand Up @@ -1126,10 +1152,13 @@ LLVMValueRef gen_ffi(compile_t* c, ast_t* ast)
// Get the function name, +1 to skip leading @
const char* f_name = ast_name(id) + 1;

deferred_reification_t* reify = c->frame->reify;

// Get the return type.
ast_t* type = ast_type(ast);
ast_t* type = deferred_reify(reify, ast_type(ast), c->opt);
reach_type_t* t = reach_type(c->reach, type);
pony_assert(t != NULL);
ast_free_unattached(type);

// Get the function. First check if the name is in use by a global and error
// if it's the case.
Expand Down Expand Up @@ -1260,7 +1289,7 @@ LLVMValueRef gen_ffi(compile_t* c, ast_t* ast)
compile_type_t* c_t = (compile_type_t*)t->c_type;

// Special case a None return value, which is used for void functions.
bool isnone = is_none(type);
bool isnone = is_none(t->ast);
bool isvoid = LLVMGetReturnType(f_type) == c->void_type;

if(isnone && isvoid)
Expand All @@ -1273,7 +1302,7 @@ LLVMValueRef gen_ffi(compile_t* c, ast_t* ast)

result = cast_ffi_arg(c, ffi_decl, ast, result, c_t->use_type,
"return values");
result = gen_assign_cast(c, c_t->use_type, result, type);
result = gen_assign_cast(c, c_t->use_type, result, t->ast_cap);

return result;
}
Expand Down
Loading