Skip to content

Commit

Permalink
🎨 define union in section
Browse files Browse the repository at this point in the history
  • Loading branch information
fennecdjay committed Jul 23, 2019
1 parent 7d840c2 commit d834002
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 153 deletions.
2 changes: 1 addition & 1 deletion ast
Submodule ast updated from f4fae7 to 195d19
8 changes: 4 additions & 4 deletions include/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static const _exp_func exp_func[] = {
#define DECL_SECTION_FUNC(prefix) \
static const _exp_func section_func[] = { \
(_exp_func)prefix##_stmt_list, (_exp_func)prefix##_func_def, (_exp_func)prefix##_class_def, \
(_exp_func)prefix##_enum_def \
(_exp_func)prefix##_enum_def, (_exp_func)prefix##_union_def \
}; \
ANN static inline m_bool prefix##_section(const void* a, /* const */ Section* section) { GWDEBUG_EXE \
void* d = &section->d.stmt_list; \
Expand All @@ -60,8 +60,8 @@ ANN static m_bool prefix##_stmt_##name(const Env env, const type stmt) { GWDEBUG
RET_NSPC(exp) \
}

ANN m_uint union_push(const Env, const Stmt_Union);
ANN void union_pop(const Env, const Stmt_Union, const m_uint);
ANN m_uint union_push(const Env, const Union_Def);
ANN void union_pop(const Env, const Union_Def, const m_uint);
ANN m_bool check_stmt(const Env env, const Stmt stmt);

ANN m_bool scanx_body(const Env env, const Class_Def cdef, const _exp_func f, void* data);
Expand All @@ -87,7 +87,7 @@ ANN m_bool scanx_cdef(const Env, void *,const Class_Def,
#define xxx_cdef(prefix) \
static inline m_bool prefix##_cdef(const Env env, const Class_Def cdef) { \
return scanx_cdef(env, env, cdef, \
(_exp_func)prefix##_class_def, (_exp_func)prefix##_stmt_union); \
(_exp_func)prefix##_class_def, (_exp_func)prefix##_union_def); \
}
xxx_cdef(scan1)
xxx_cdef(scan2)
Expand Down
10 changes: 5 additions & 5 deletions include/traverse.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
ANN m_bool traverse_ast(const Env, const Ast);
ANN m_bool traverse_class_def(const Env, const Class_Def);
ANN m_bool traverse_func_def(const Env, const Func_Def);
ANN m_bool traverse_stmt_union(const Env, const Stmt_Union);
ANN m_bool traverse_union_def(const Env, const Union_Def);
ANN m_bool traverse_enum_def(const Env, const Enum_Def);
ANN m_bool traverse_stmt_fptr(const Env, const Stmt_Fptr );
ANN m_bool traverse_stmt_type(const Env env, const Stmt_Type def);
Expand All @@ -27,10 +27,10 @@ ANN m_bool scan1_stmt_fptr(const Env, const Stmt_Fptr);
ANN m_bool scan2_stmt_fptr(const Env, const Stmt_Fptr);
//ANN m_bool check_stmt_fptr(const Env, const Stmt_Fptr);

ANN m_bool scan0_stmt_union(const Env, const Stmt_Union);
ANN m_bool scan1_stmt_union(const Env, const Stmt_Union);
ANN m_bool scan2_stmt_union(const Env, const Stmt_Union);
ANN m_bool check_stmt_union(const Env, const Stmt_Union);
ANN m_bool scan0_union_def(const Env, const Union_Def);
ANN m_bool scan1_union_def(const Env, const Union_Def);
ANN m_bool scan2_union_def(const Env, const Union_Def);
ANN m_bool check_union_def(const Env, const Union_Def);

ANN m_bool scan0_enum_def(const Env, const Enum_Def);
ANN m_bool scan1_enum_def(const Env, const Enum_Def);
Expand Down
66 changes: 33 additions & 33 deletions src/emit/emit.c
Original file line number Diff line number Diff line change
Expand Up @@ -1364,59 +1364,59 @@ ANN void emit_union_offset(Decl_List l, const m_uint o) {
} while((l = l->next));
}

ANN static inline void union_allocdata(MemPool mp, const Stmt_Union stmt) {
const Nspc nspc = (stmt->xid ? stmt->value->type : stmt->type)->nspc;
ANN static inline void union_allocdata(MemPool mp, const Union_Def udef) {
const Nspc nspc = (udef->xid ? udef->value->type : udef->type)->nspc;
nspc_allocdata(mp, nspc);
nspc->info->offset = stmt->s;
nspc->info->offset = udef->s;
}

ANN static m_bool emit_stmt_union(const Emitter emit, const Stmt_Union stmt) {
if(stmt->tmpl)
ANN static m_bool emit_union_def(const Emitter emit, const Union_Def udef) {
if(tmpl_base(udef->tmpl))
return GW_OK;
Decl_List l = stmt->l;
Decl_List l = udef->l;
m_uint scope = emit->env->scope->depth;
const m_bool global = GET_FLAG(stmt, global);
if(stmt->xid) {
union_allocdata(emit->gwion->mp, stmt);
const m_bool global = GET_FLAG(udef, global);
if(udef->xid) {
union_allocdata(emit->gwion->mp, udef);
Type_Decl *type_decl = new_type_decl(emit->gwion->mp,
new_id_list(emit->gwion->mp, stmt->xid, loc_cpy(emit->gwion->mp, stmt_self(stmt)->pos)));
type_decl->flag = stmt->flag;
const Var_Decl var_decl = new_var_decl(emit->gwion->mp, stmt->xid, NULL, loc_cpy(emit->gwion->mp, stmt_self(stmt)->pos));
new_id_list(emit->gwion->mp, udef->xid, udef->pos));
type_decl->flag = udef->flag;
const Var_Decl var_decl = new_var_decl(emit->gwion->mp, udef->xid, NULL, loc_cpy(emit->gwion->mp, udef->pos));
const Var_Decl_List var_decl_list = new_var_decl_list(emit->gwion->mp, var_decl, NULL);
const Exp exp = new_exp_decl(emit->gwion->mp, type_decl, var_decl_list);
exp->d.exp_decl.type = stmt->value->type;
var_decl->value = stmt->value;
exp->d.exp_decl.type = udef->value->type;
var_decl->value = udef->value;
CHECK_BB(emit_exp_decl(emit, &exp->d.exp_decl))
free_exp(emit->gwion->mp, exp);
if(global) {
const M_Object o = new_object(emit->gwion->mp, NULL, stmt->value->type);
stmt->value->d.ptr = (m_uint*)o;
SET_FLAG(stmt->value, builtin);
SET_FLAG(stmt->value, global);
const M_Object o = new_object(emit->gwion->mp, NULL, udef->value->type);
udef->value->d.ptr = (m_uint*)o;
SET_FLAG(udef->value, builtin);
SET_FLAG(udef->value, global);
}
scope = emit_push_type(emit, stmt->value->type);
} else if(stmt->type_xid) {
union_allocdata(emit->gwion->mp, stmt);
scope = emit_push_type(emit, stmt->type);
scope = emit_push_type(emit, udef->value->type);
} else if(udef->type_xid) {
union_allocdata(emit->gwion->mp, udef);
scope = emit_push_type(emit, udef->type);
} else if(emit->env->class_def) {
if(!GET_FLAG(l->self->d.exp_decl.list->self->value, member))
stmt->o = emit_local(emit, stmt->s, 0);
udef->o = emit_local(emit, udef->s, 0);
} else if(global) {
void* ptr = (void*)xcalloc(1, stmt->s);
l = stmt->l;
void* ptr = (void*)xcalloc(1, udef->s);
l = udef->l;
do {
Var_Decl_List list = l->self->d.exp_decl.list;
list->self->value->d.ptr = ptr;
SET_FLAG(list->self->value, union);
} while((l = l->next));
SET_FLAG(stmt->l->self->d.exp_decl.list->self->value, enum);
SET_FLAG(udef->l->self->d.exp_decl.list->self->value, enum);
}
if(stmt->xid)
regpop(emit, !GET_FLAG(stmt, static) ? SZ_INT : SZ_INT*2);
emit_union_offset(stmt->l, stmt->o);
if(stmt->xid || stmt->type_xid || global)
if(udef->xid)
regpop(emit, !GET_FLAG(udef, static) ? SZ_INT : SZ_INT*2);
emit_union_offset(udef->l, udef->o);
if(udef->xid || udef->type_xid || global)
emit_pop(emit, scope);
SET_FLAG(stmt->xid ? stmt->value->type : stmt->type, emit);
SET_FLAG(udef->xid ? udef->value->type : udef->type, emit);
return GW_OK;
}

Expand All @@ -1430,7 +1430,7 @@ static const _exp_func stmt_func[] = {
(_exp_func)emit_stmt_if, (_exp_func)emit_stmt_code, (_exp_func)emit_stmt_switch,
(_exp_func)emit_stmt_break, (_exp_func)emit_stmt_continue, (_exp_func)emit_stmt_return,
(_exp_func)emit_stmt_case, (_exp_func)emit_stmt_jump,
(_exp_func)dummy_func, (_exp_func)emit_stmt_type, (_exp_func)emit_stmt_union,
(_exp_func)dummy_func, (_exp_func)emit_stmt_type
};

ANN static m_bool emit_stmt(const Emitter emit, const Stmt stmt, const m_bool pop) {
Expand Down Expand Up @@ -1742,7 +1742,7 @@ ANN static m_bool emit_parent(const Emitter emit, const Class_Def cdef) {

ANN static inline m_bool emit_cdef(const Emitter emit, const Class_Def cdef) {
return scanx_cdef(emit->env, emit, cdef,
(_exp_func)emit_class_def, (_exp_func)emit_stmt_union);
(_exp_func)emit_class_def, (_exp_func)emit_union_def);
}

ANN static m_bool emit_class_def(const Emitter emit, const Class_Def cdef) {
Expand Down
18 changes: 9 additions & 9 deletions src/lib/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -599,16 +599,16 @@ ANN m_int gwi_union_add(const Gwi gwi, const restrict m_str type, const restrict
ANN Type gwi_union_end(const Gwi gwi, const ae_flag flag) {
if(!gwi->union_data.list)
GWI_ERR_O(_("union is empty"));
const Stmt stmt = new_stmt_union(gwi->gwion->mp, gwi->union_data.list, loc_cpy(gwi->gwion->mp, gwi->loc));
stmt->d.stmt_union.flag = flag;
CHECK_BO(traverse_stmt_union(gwi->gwion->env, &stmt->d.stmt_union))
emit_union_offset(stmt->d.stmt_union.l, stmt->d.stmt_union.o);
if(GET_FLAG((&stmt->d.stmt_union), member))
const Union_Def udef = new_union_def(gwi->gwion->mp, gwi->union_data.list, loc_cpy(gwi->gwion->mp, gwi->loc));
udef->flag = flag;
CHECK_BO(traverse_union_def(gwi->gwion->env, udef))
emit_union_offset(udef->l, udef->o);
if(GET_FLAG(udef, member))
gwi->gwion->env->class_def->nspc->info->offset =
stmt->d.stmt_union.o + stmt->d.stmt_union.s;
const Type t = stmt->d.stmt_union.xid ? stmt->d.stmt_union.value->type :
stmt->d.stmt_union.type_xid ? stmt->d.stmt_union.type : t_int;
free_stmt(gwi->gwion->mp, stmt);
udef->o + udef->s;
const Type t = udef->xid ? udef->value->type :
udef->type_xid ? udef->type : t_int;
free_union_def(gwi->gwion->mp, udef);
gwi->union_data.list = NULL;
gwi->union_data.xid = NULL;
return t;
Expand Down
8 changes: 4 additions & 4 deletions src/oo/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
ANN static void free_type(Type a, Gwion gwion) {
if(GET_FLAG(a, template)) {
if(GET_FLAG(a, union)) {
if(a->e->def->stmt && !GET_FLAG(a, pure)) { // <=> decl_list
UNSET_FLAG(&a->e->def->stmt->d.stmt_union, global);
free_stmt(gwion->mp, a->e->def->stmt);
if(a->e->def->union_def && !GET_FLAG(a, pure)) { // <=> decl_list
UNSET_FLAG(a->e->def->union_def, global);
free_union_def(gwion->mp, a->e->def->union_def);
}
a->e->def->stmt = NULL;
a->e->def->union_def = NULL;
} else
free_class_def(gwion->mp, a->e->def);
}
Expand Down
28 changes: 14 additions & 14 deletions src/parse/check.c
Original file line number Diff line number Diff line change
Expand Up @@ -1045,36 +1045,36 @@ ANN static m_bool check_stmt_jump(const Env env, const Stmt_Jump stmt) {
return GW_OK;
}

ANN m_bool check_stmt_union(const Env env, const Stmt_Union stmt) {
if(stmt->tmpl && stmt->tmpl->base == -1) // there's a func for this
ANN m_bool check_union_def(const Env env, const Union_Def udef) {
if(tmpl_base(udef->tmpl)) // there's a func for this
return GW_OK;
if(stmt->xid) {
if(udef->xid) {
if(env->class_def)
(!GET_FLAG(stmt, static) ? decl_member : decl_static)(env->curr, stmt->value);
(!GET_FLAG(udef, static) ? decl_member : decl_static)(env->curr, udef->value);
} else if(env->class_def) {
if(!GET_FLAG(stmt, static))
stmt->o = env->class_def->nspc->info->offset;
if(!GET_FLAG(udef, static))
udef->o = env->class_def->nspc->info->offset;
else {
stmt->o = env->class_def->nspc->info->class_data_size;
udef->o = env->class_def->nspc->info->class_data_size;
env->class_def->nspc->info->class_data_size += SZ_INT;
}
}
const m_uint scope = union_push(env, stmt);
Decl_List l = stmt->l;
const m_uint scope = union_push(env, udef);
Decl_List l = udef->l;
do {
CHECK_OB(check_exp(env, l->self))
if(isa(l->self->type, t_object) > 0) {
if(!GET_FLAG(l->self->d.exp_decl.td, ref) && !GET_FLAG(stmt->type, template))
if(!GET_FLAG(l->self->d.exp_decl.td, ref) && !GET_FLAG(udef->type, template))
ERR_B(l->self->pos, _("In union, Objects must be declared as reference (use '@')"))
// SET_FLAG(l->self->d.exp_decl.td, ref);
Var_Decl_List list = l->self->d.exp_decl.list;
do SET_FLAG(list->self->value, pure);
while((list = list->next));
}
if(l->self->type->size > stmt->s)
stmt->s = l->self->type->size;
if(l->self->type->size > udef->s)
udef->s = l->self->type->size;
} while((l = l->next));
union_pop(env, stmt, scope);
union_pop(env, udef, scope);
return GW_OK;
}

Expand All @@ -1088,7 +1088,7 @@ static const _exp_func stmt_func[] = {
(_exp_func)check_stmt_if, (_exp_func)check_stmt_code, (_exp_func)check_stmt_switch,
(_exp_func)check_stmt_break, (_exp_func)check_stmt_continue, (_exp_func)check_stmt_return,
(_exp_func)check_stmt_case, (_exp_func)check_stmt_jump,
(_exp_func)dummy_func, (_exp_func)check_stmt_type, (_exp_func)check_stmt_union,
(_exp_func)dummy_func, (_exp_func)check_stmt_type
};

ANN m_bool check_stmt(const Env env, const Stmt stmt) {
Expand Down
10 changes: 6 additions & 4 deletions src/parse/cpy_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ ANN static void cpy_stmt_type(MemPool p, Stmt_Type a, const Stmt_Type src) {
a->tmpl = cpy_tmpl(p, src->tmpl);
}

ANN static void cpy_stmt_union(MemPool p, Stmt_Union a,const Stmt_Union src) {
ANN static Union_Def cpy_union_def(MemPool p, const Union_Def src) {
Union_Def a = mp_calloc(p, Union_Def);
a->l = cpy_decl_list(p, src->l); // 1
if(src->xid)
a->xid = src->xid; // 1
Expand All @@ -354,6 +355,7 @@ ANN static void cpy_stmt_union(MemPool p, Stmt_Union a,const Stmt_Union src) {
if(src->tmpl)
a->tmpl = cpy_tmpl(p, src->tmpl); // 1
a->flag = src->flag; // 1
return a;
}

ANN static Stmt cpy_stmt(MemPool p, const Stmt src) {
Expand Down Expand Up @@ -401,9 +403,6 @@ ANN static Stmt cpy_stmt(MemPool p, const Stmt src) {
case ae_stmt_type:
cpy_stmt_type(p, &a->d.stmt_type, &src->d.stmt_type);
break;
case ae_stmt_union:
cpy_stmt_union(p, &a->d.stmt_union, &src->d.stmt_union);
break;
case ae_stmt_break:
case ae_stmt_continue:
break;
Expand Down Expand Up @@ -449,6 +448,9 @@ ANN static Section* cpy_section(MemPool p, const Section *src) {
case ae_section_enum:
a->d.enum_def = cpy_enum_def(p, src->d.enum_def);
break;
case ae_section_union:
a->d.union_def = cpy_union_def(p, src->d.union_def);
break;
}
a->section_type = src->section_type;
return a;
Expand Down

0 comments on commit d834002

Please sign in to comment.