Skip to content

Commit

Permalink
Fix DllMain and only call main on DLL_PROCESS_ATTACH
Browse files Browse the repository at this point in the history
  • Loading branch information
gingerBill committed Dec 9, 2016
1 parent e8b4228 commit de9016b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 21 deletions.
62 changes: 41 additions & 21 deletions src/ssa.c
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,8 @@ ssaValue *ssa_emit_comp(ssaProcedure *proc, TokenKind op_kind, ssaValue *left, s
Type *a = base_type(ssa_type(left));
Type *b = base_type(ssa_type(right));

GB_ASSERT(gb_is_between(op_kind, Token__ComparisonBegin+1, Token__ComparisonEnd-1));

if (are_types_identical(a, b)) {
// NOTE(bill): No need for a conversion
} else if (left->kind == ssaValue_Constant || left->kind == ssaValue_Nil) {
Expand Down Expand Up @@ -3266,6 +3268,28 @@ bool ssa_is_elem_const(ssaModule *m, AstNode *elem, Type *elem_type) {
return tav->value.kind != ExactValue_Invalid;
}

ssaAddr ssa_build_addr_from_entity(ssaProcedure *proc, Entity *e, AstNode *expr) {
GB_ASSERT(e != NULL);
GB_ASSERT(e->kind != Entity_Constant);

ssaValue *v = NULL;
ssaValue **found = map_ssa_value_get(&proc->module->values, hash_pointer(e));
if (found) {
v = *found;
} else if (e->kind == Entity_Variable && e->flags & EntityFlag_Anonymous) {
v = ssa_add_using_variable(proc, e);
} else if (e->kind == Entity_ImplicitValue) {
// TODO(bill): Should a copy be made?
v = ssa_find_implicit_value_backing(proc, e->ImplicitValue.id);
}

if (v == NULL) {
GB_PANIC("Unknown value: %s, entity: %p %.*s\n", expr_to_string(expr), e, LIT(entity_strings[e->kind]));
}

return ssa_make_addr(v, expr);
}

ssaAddr ssa_build_addr(ssaProcedure *proc, AstNode *expr) {
switch (expr->kind) {
case_ast_node(i, Ident, expr);
Expand All @@ -3275,26 +3299,7 @@ ssaAddr ssa_build_addr(ssaProcedure *proc, AstNode *expr) {
}

Entity *e = entity_of_ident(proc->module->info, expr);
TypeAndValue *tv = map_tav_get(&proc->module->info->types, hash_pointer(expr));

GB_ASSERT(e->kind != Entity_Constant);

ssaValue *v = NULL;
ssaValue **found = map_ssa_value_get(&proc->module->values, hash_pointer(e));
if (found) {
v = *found;
} else if (e->kind == Entity_Variable && e->flags & EntityFlag_Anonymous) {
v = ssa_add_using_variable(proc, e);
} else if (e->kind == Entity_ImplicitValue) {
// TODO(bill): Should a copy be made?
v = ssa_find_implicit_value_backing(proc, e->ImplicitValue.id);
}

if (v == NULL) {
GB_PANIC("Unknown value: %s, entity: %p %.*s\n", expr_to_string(expr), e, LIT(entity_strings[e->kind]));
}

return ssa_make_addr(v, expr);
return ssa_build_addr_from_entity(proc, e, expr);
case_end;

case_ast_node(pe, ParenExpr, expr);
Expand Down Expand Up @@ -5035,7 +5040,7 @@ void ssa_gen_tree(ssaGen *s) {
proc_results->Tuple.variable_count = 1;

proc_params->Tuple.variables[0] = make_entity_param(a, proc_scope, blank_token, t_rawptr, false);
proc_params->Tuple.variables[1] = make_entity_param(a, proc_scope, blank_token, t_u32, false);
proc_params->Tuple.variables[1] = make_entity_param(a, proc_scope, make_token_ident(str_lit("reason")), t_i32, false);
proc_params->Tuple.variables[2] = make_entity_param(a, proc_scope, blank_token, t_rawptr, false);

proc_results->Tuple.variables[0] = make_entity_param(a, proc_scope, empty_token, t_i32, false);
Expand All @@ -5057,8 +5062,23 @@ void ssa_gen_tree(ssaGen *s) {
e->Procedure.link_name = name;

ssa_begin_procedure_body(proc);

// NOTE(bill): https://msdn.microsoft.com/en-us/library/windows/desktop/ms682583(v=vs.85).aspx
// DLL_PROCESS_ATTACH == 1

ssaAddr reason_addr = ssa_build_addr_from_entity(proc, proc_params->Tuple.variables[1], NULL);
ssaValue *cond = ssa_emit_comp(proc, Token_CmpEq, ssa_addr_load(proc, reason_addr), v_one32);
ssaBlock *then = ssa_add_block(proc, NULL, "if.then");
ssaBlock *done = ssa_add_block(proc, NULL, "if.done"); // NOTE(bill): Append later
ssa_emit_if(proc, cond, then, done);
proc->curr_block = then;
ssa_emit_global_call(proc, "main", NULL, 0);
ssa_emit_jump(proc, done);
proc->curr_block = done;

ssa_emit_return(proc, v_one32);


ssa_end_procedure_body(proc);
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions src/ssa_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -993,6 +993,7 @@ void ssa_print_instr(ssaFileBuffer *f, ssaModule *m, ssaValue *value) {
case Token_Gt: ssa_fprintf(f, "gt"); break;
case Token_LtEq: ssa_fprintf(f, "le"); break;
case Token_GtEq: ssa_fprintf(f, "ge"); break;
default: GB_PANIC("invalid comparison");break;
}
}
} else {
Expand Down Expand Up @@ -1281,6 +1282,8 @@ void ssa_print_proc(ssaFileBuffer *f, ssaModule *m, ssaProcedure *proc) {
if (!str_eq(e->token.string, str_lit("")) &&
!str_eq(e->token.string, str_lit("_"))) {
ssa_fprintf(f, " %%%.*s", LIT(e->token.string));
} else {
ssa_fprintf(f, " %%_.param_%td", i);
}
}
}
Expand Down

0 comments on commit de9016b

Please sign in to comment.