// optimizations
#ifdef OPTIMIZE
INSTR(PutArgsInMem);
-INSTR(ConstPropSet);
-INSTR(ConstPropGet);
#endif
#include "opcode.h"
#endif
#ifndef __OPTIM
#define __OPTIM
#ifdef OPTIMIZE
-#define OPTIMIZE_DEFAULT default: break;
#define OPTIMIZE_CONST(a) CHECK_BO(optimize_const(a))
ANN m_bool optimize_const(const Exp_Binary*);
ANN2(1) void constprop_prim(const Exp_Primary* p, m_uint* ptr);
-#define OPTIMIZE_PRIM_CONST(a, b) \
-else if(GET_FLAG(v, ae_flag_constprop)) \
- constprop_prim(a,b);
#else
-#define OPTIMIZE_DEFAULT
-#define OPTIMIZE_CONST(a)
-#define OPTIMIZE_PRIM_CONST(a, b)
#endif
m_bool constant_folding(const Exp_Binary* bin);
#endif
#include "type.h"
#include "constant.h"
-#define describe_constant_xxx(name, etype, mtype) \
-ANN m_bool constant_##name(const Exp e) { \
- return (e->exp_type == ae_exp_primary && \
- e->d.exp_primary.primary_type == ae_primary_##etype) || \
- (e->exp_type == ae_exp_constprop && isa(e->type , mtype) > 0); \
+#define describe_constant_xxx(name, etype, mtype) \
+ANN m_bool constant_##name(const Exp e) { \
+ return (e->exp_type == ae_exp_primary && \
+ e->d.exp_primary.primary_type == ae_primary_##etype); \
}
describe_constant_xxx(int, num, t_int)
describe_constant_xxx(float, float, t_float)
#include "optim.h"
#include "constant.h"
-void constprop_prim(const Exp_Primary* p, m_uint* ptr) {
- Exp_Primary* e = (Exp_Primary*)p;
- e->primary_type = ae_primary_constprop;
- e->d.num = (m_uint)ptr; // improve me
-}
-
-ANN static m_bool is_constprop_value(const Exp e) {
- if (e->exp_type == ae_exp_decl
- && isa(e->d.exp_decl.list->self->value->type, t_int) > 0 &&
- !e->d.exp_decl.list->self->value->type->array_depth &&
- !e->d.exp_decl.list->self->value->owner_class &&
- !GET_FLAG(e->d.exp_decl.list->self->value, arg))
- return (e->exp_type == ae_exp_primary &&
- e->d.exp_primary.primary_type == ae_primary_id &&
- isa(e->type, t_int) > 0 &&
- !e->d.exp_primary.value->owner_class) ||
- e->exp_type == ae_exp_constprop2;
- return 0;
-}
-
-ANN static void constprop_exp(const Exp_Binary* bin, long num) {
- const Exp e = bin->self, l = bin->lhs, r = bin->rhs;
- e->exp_type = ae_exp_constprop;
- e->d.exp_primary.d.num = num;
- free_exp(l);
- free_exp(r);
-}
-
-ANN static void constprop_value(const Value v, const long num) {
- v->d.ptr = (m_uint*)num; //fixme
- SET_FLAG(v, constprop);
-}
-
-ANN static m_bool constant_propagation(const Exp_Binary* bin) {
- const Exp l = bin->lhs, r = bin->rhs;
- switch(bin->op) {
- case op_chuck:
- if(isa(r->type, t_function) < 0) {
- if(is_constprop_value(r)) {
- if(constant_int(l)) {
-if(r->d.exp_primary.primary_type == ae_primary_num) {
- constprop_value(r->d.exp_primary.value,
- l->d.exp_primary.d.num);
- constprop_exp(bin, l->d.exp_primary.d.num);
-}
- else if(r->exp_type == ae_exp_decl) {
- SET_FLAG(r->d.exp_decl.list->self->value, constprop);
- *(m_uint*)r->d.exp_decl.list->self->value->d.ptr = l->d.exp_primary.d.num;
- }
- return GW_OK;
- }
- }
- } /* fallthrough */
- case op_radd:
- case op_rsub:
- case op_rmul:
- case op_rdiv:
- case op_rmod:
- case op_rsl:
- case op_rsr:
- case op_rsand:
- case op_rsor:
- case op_rsxor:
- case op_ref:
- case op_unref:
- case op_trig:
- case op_untrig:
- if(r->exp_type == ae_exp_constprop2) {
- r->d.exp_primary.value->d.ptr = 0;
- UNSET_FLAG(r->d.exp_primary.value, constprop);
- }
- default: break;
- }
- return GW_OK;
-}
-
m_bool optimize_const(const Exp_Binary* bin) {
- constant_propagation(bin);
return constant_folding(bin);
}
const m_uint size = v->type->size;
const Instr instr = emit_kind(emit, size, prim->self->emit_var, regpushimm);
if(!prim->self->emit_var && size == SZ_INT) {
- if(isa(v->type, t_object) > 0) {
- instr->execute = RegPushImm;
- instr->m_val = (m_uint)v->d.ptr;
- } else if(v->d.ptr)
- instr->m_val = *(m_uint*)v->d.ptr;
+ if(isa(v->type, t_object) > 0) {
+ instr->execute = RegPushImm;
+ instr->m_val = (m_uint)v->d.ptr;
+ } else if(v->d.ptr)
+ instr->m_val = *(m_uint*)v->d.ptr;
} else if(v->d.ptr)
memcpy(&instr->m_val, v->d.ptr, v->type->size);
else
return GW_OK;
}
-#ifdef OPTIMIZE
-ANN static m_bool prim_constprop(Emitter emit, const Exp_Primary* prim) {
- const Instr instr = emit_add_instr(emit, ConstPropSet);
- instr->m_val = prim->value->offset;
- instr->m_val2 = prim->d.num;
- *(m_bool*)instr->ptr = prim->self->emit_var;
- return GW_OK;
-}
-#endif
-
static const _exp_func prim_func[] = {
(_exp_func)prim_id, (_exp_func)prim_num, (_exp_func)prim_float, (_exp_func)prim_str,
(_exp_func)prim_array, (_exp_func)prim_gack, (_exp_func)prim_vec, (_exp_func)prim_vec,
(_exp_func)prim_vec, (_exp_func)prim_char, (_exp_func)dummy_func,
-#ifdef OPTIMIZE
- (_exp_func)prim_constprop
-#endif
};
ANN static m_bool emit_exp_primary(const Emitter emit, const Exp_Primary* prim) { GWDEBUG_EXE
return ret;
}
-#ifdef OPTIMIZE
-ANN static void emit_exp_constprop(const Emitter emit, const Exp e) {
- if(!e->emit_var) {
- if(e->exp_type == ae_exp_constprop) {
- const Instr instr = emit_add_instr(emit, RegPushMem);
- instr->m_val = e->d.exp_primary.value->offset;
- *(m_uint*)instr->ptr = GET_FLAG(e->d.exp_primary.value, global);
- return;
- }
- const Instr instr = emit_add_instr(emit, ConstPropGet);
- instr->m_val2 = e->d.exp_primary.value->offset;
- instr->m_val = e->d.exp_primary.d.num;
- *(m_uint*)instr->ptr = 1;
- } else {
- const Instr instr = emit_add_instr(emit, ConstPropSet);
- instr->m_val = e->d.exp_primary.value->offset;
- *(m_bool*)instr->ptr = 1;
- instr->m_val2 = e->d.exp_primary.d.num;
- }
-}
-#endif
-
DECL_EXP_FUNC(emit)
ANN2(1) static m_bool emit_exp(const Emitter emit, Exp exp, const m_bool ref) { GWDEBUG_EXE
POP_REG(shred, instr->m_val)
memcpy(shred->mem, shred->reg, instr->m_val);
}
-
-INSTR(ConstPropSet) { GWDEBUG_EXE
- if(*(m_uint*)instr->ptr >= 2) {
- *(m_int**)(shred->reg) = (m_int*)(shred->mem + instr->m_val);
- PUSH_REG(shred, SZ_INT);
- return;
- }
- *(m_int*)(shred->mem + instr->m_val) = instr->m_val2;
- if(*(m_bool*)instr->ptr)
- *(m_int**)(shred->reg) = (m_int*)(shred->mem + instr->m_val);
- else
- *(m_int*)(shred->reg) = instr->m_val2;
- PUSH_REG(shred, SZ_INT);
- *(m_uint*)instr->ptr = 2 + *(m_uint*)instr->ptr;
-}
-
-INSTR(ConstPropGet) { GWDEBUG_EXE
- if(!*(m_uint*)instr->ptr)
- memcpy(REG(0), MEM(instr->m_val2), SZ_INT);
- else
- memcpy(REG(0), &instr->m_val, SZ_INT);
- PUSH_REG(shred, SZ_INT);
-}
#endif
Type_List tmpl_tl(const Env env, const m_str name) {
OP_CHECK(opck_unary_meta) {
const Exp_Unary* unary = (Exp_Unary*)data;
unary->self->meta = ae_meta_value;
-#ifdef OPTIMIZE
- if(unary->exp->exp_type == ae_exp_constprop2) {
- unary->exp->exp_type =ae_exp_primary;
- unary->exp->d.exp_primary.primary_type = ae_primary_id;
- unary->exp->d.exp_primary.d.num = (m_uint)unary->exp->d.exp_primary.value->d.ptr;
- UNSET_FLAG(unary->exp->d.exp_primary.value, constprop);
- unary->exp->d.exp_primary.value->d.ptr = 0;
- }
-#endif
return unary->exp->type;
}
op2str(unary->op), access(unary->exp->meta))
unary->exp->emit_var = 1;
unary->self->meta = ae_meta_value;
-#ifdef OPTIMIZE
-if(unary->exp->exp_type == ae_exp_primary &&
- GET_FLAG(unary->exp->d.exp_primary.value, constprop)) {
- UNSET_FLAG(unary->exp->d.exp_primary.value, constprop);
- unary->exp->d.exp_primary.value->d.ptr = 0;
- return unary->exp->type;
-}
- if(unary->exp->exp_type == ae_exp_constprop) {
- unary->exp->exp_type = ae_exp_primary;
- unary->exp->d.exp_primary.primary_type = ae_primary_constprop;
- unary->exp->d.exp_primary.d.num = (m_uint)unary->exp->d.exp_primary.value->d.ptr;
- UNSET_FLAG(unary->exp->d.exp_primary.value, constprop);
- unary->exp->d.exp_primary.value->d.ptr = 0;
- }
-#endif
return unary->exp->type;
}
op2str(post->op), access(post->exp->meta))
post->exp->emit_var = 1;
post->self->meta = ae_meta_value;
-#ifdef OPTIMIZE
-if(post->exp->exp_type == ae_exp_primary &&
- GET_FLAG(post->exp->d.exp_primary.value, constprop)) {
- UNSET_FLAG(post->exp->d.exp_primary.value, constprop);
- post->exp->d.exp_primary.value->d.ptr = 0;
- return post->exp->type;
-}
- if(post->exp->exp_type == ae_exp_constprop2) {exit(3);
- post->exp->exp_type =ae_exp_primary;
- post->exp->d.exp_primary.primary_type = ae_primary_constprop;
- post->exp->d.exp_primary.d.num = (m_uint)post->exp->d.exp_primary.value->d.ptr;
- UNSET_FLAG(post->exp->d.exp_primary.value, constprop);
- post->exp->d.exp_primary.value->d.ptr = 0;
-}
- if(post->exp->exp_type == ae_exp_constprop) {exit(2);
- post->exp->exp_type =ae_exp_primary;
- post->exp->d.exp_primary.primary_type = ae_primary_constprop;
- post->exp->d.exp_primary.d.num = (m_uint)post->exp->d.exp_primary.value->d.ptr;
- UNSET_FLAG(post->exp->d.exp_primary.value, constprop);
- post->exp->d.exp_primary.value->d.ptr = 0;
-}
-#endif
return post->exp->type;
}
#include "type.h"
ANN static void free_value(Value a) {
-// if(!GET_FLAG(a, func) && !GET_FLAG(a, constprop) && a->d.ptr && isa(a->type, t_object) < 0)
- if(!GET_FLAG(a, func) && !GET_FLAG(a, constprop) && a->d.ptr &&
+ if(!GET_FLAG(a, func) && a->d.ptr &&
!(GET_FLAG(a, enum) && GET_FLAG(a, builtin) && a->owner_class)
&& isa(a->type, t_object) < 0)
_mp_free(a->type->size, a->d.ptr);