Skip to content

Commit

Permalink
On master: some work
Browse files Browse the repository at this point in the history
  • Loading branch information
fennecdjay committed May 3, 2024
2 parents e523855 + 1f8323e commit 7e09034
Show file tree
Hide file tree
Showing 37 changed files with 218 additions and 118 deletions.
16 changes: 8 additions & 8 deletions include/compile.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#ifndef __COMPILE
#define __COMPILE
m_uint compile_filename_values(struct Gwion_ *vm, const m_str filename, MP_Vector*);
m_uint compile_string_values(struct Gwion_ *vm, const m_str filename,
const m_str data, MP_Vector*);
m_uint compile_file_values(struct Gwion_ *vm, const m_str filename, FILE *file, MP_Vector*);
m_uint compile_filename_xid_values(struct Gwion_ *vm, const m_str filename,
m_uint compile_filename_values(struct Gwion_ *vm, const char * filename, MP_Vector*);
m_uint compile_string_values(struct Gwion_ *vm, const char * filename,
const char * data, MP_Vector*);
m_uint compile_file_values(struct Gwion_ *vm, const char * filename, FILE *file, MP_Vector*);
m_uint compile_filename_xid_values(struct Gwion_ *vm, const char * filename,
const m_uint xid, MP_Vector*);
m_uint compile_string_xid_values(struct Gwion_ *vm, const m_str filename,
const m_str data, const m_uint xid, MP_Vector*);
m_uint compile_file_xid_values(struct Gwion_ *vm, const m_str filename, FILE *file,
m_uint compile_string_xid_values(struct Gwion_ *vm, const char * filename,
const char * data, const m_uint xid, MP_Vector*);
m_uint compile_file_xid_values(struct Gwion_ *vm, const char * filename, FILE *file,
const m_uint xid, MP_Vector*);
#define compile_filename(a, b) compile_filename_values(a, b, NULL)
#define compile_string(a, b, c) compile_string_values(a, b, c, NULL)
Expand Down
5 changes: 4 additions & 1 deletion include/emit.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,10 @@ ANN VM_Code finalyze(const Emitter emit, const f_instr exec);
ANN static inline void emit_push_code(const Emitter emit, const m_str name) {
vector_add(&emit->stack, (vtype)emit->code);
emit->code = new_code(emit, name);
if (emit->info->debug) emit_add_instr(emit, DebugLine);
if (emit->info->debug) {
const Instr instr = emit_add_instr(emit, DebugLine);
instr->m_val = emit->status.line;
}
}

ANN static inline void emit_pop_code(const Emitter emit) {
Expand Down
7 changes: 3 additions & 4 deletions include/env/type.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,9 @@ struct Type_ {
m_str name;
Nspc nspc;
struct TypeInfo_ *info;
uint64_t size;
uint64_t actual_size;
// struct Vector_ effects; // pre-ctor effects
uint32_t array_depth;
uint32_t size;
uint32_t actual_size;
uint32_t array_depth; // TODO: encode in array type
uint16_t ref;
uint16_t weight;
ae_flag flag;
Expand Down
2 changes: 1 addition & 1 deletion include/import/tdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
ANN bool gwi_typedef_ini(const Gwi gwi, const restrict m_str type,
const restrict m_str name);
ANN bool gwi_typedef_exp(const Gwi gwi, const restrict m_str data);
ANN Type gwi_typedef_end(const Gwi gwi, const ae_flag flag);
ANN bool gwi_typedef_end(const Gwi gwi, const ae_flag flag);
ANN void ck_clean_tdef(MemPool, ImportCK *);

ANN Type mk_primitive(const Env env, const m_str name, const m_uint size);
Expand Down
1 change: 1 addition & 0 deletions include/lang_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ANN bool gwimport_shred(const Gwi gwi);
ANN bool gwimport_event(const Gwi gwi);
ANN bool gwimport_ugen(const Gwi gwi);
ANN bool gwimport_array(const Gwi gwi);
ANN bool gwimport_vector(const Gwi gwi);
ANN bool gwimport_xork(const Gwi gwi);
ANN bool gwimport_modules(const Gwi gwi);
ANN bool gwimport_object_op(const Gwi gwi);
Expand Down
6 changes: 0 additions & 6 deletions include/opcode.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,6 @@ enum {
ePerformEffect,
eNoOp,
eDebugLine,
eDebugValue,
eDebugPush,
eDebugPop,
eEOC,
Expand Down Expand Up @@ -437,7 +436,6 @@ enum {
#define PerformEffect (f_instr)ePerformEffect
#define NoOp (f_instr)eNoOp
#define DebugLine (f_instr)eDebugLine
#define DebugValue (f_instr)eDebugValue
#define DebugPush (f_instr)eDebugPush
#define DebugPop (f_instr)eDebugPop
#define EOC (f_instr)eEOC
Expand Down Expand Up @@ -1449,10 +1447,6 @@ ANN static inline void dump_opcodes(const VM_Code code) {
gw_out(" {-R}%-14"UINT_F"{0}", instr->m_val);
gw_out("\n");
break;
case eDebugValue:
gw_out("{Y}┃{0}{-}% 4lu{0}: DebugValue ", j);
gw_out("\n");
break;
case eDebugPush:
gw_out("{Y}┃{0}{-}% 4lu{0}: DebugPush ", j);
gw_out("\n");
Expand Down
4 changes: 2 additions & 2 deletions include/operator.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ ANN bool add_op(const Gwion gwion, const struct Op_Import *);
ANN void* op_get(const Env env, struct Op_Import *opi);
ANN Type op_check(const Env, struct Op_Import *);
ANN bool op_emit(const Emitter, const struct Op_Import *);
ANN bool operator_set_func(const struct Op_Import *);
ANN bool operator_set_func(const Env, const struct Op_Import *);
ANN void free_op_map(Map map, struct Gwion_ *gwion);
ANN void free_op_tmpl(Vector v, struct Gwion_ *gwion);

ANN void operator_suspend(const Nspc, struct Op_Import *);
ANN void operator_suspend(const Gwion, struct Op_Import *);
ANN static inline void operator_resume(struct Op_Import *opi) {
assert(opi->ret);
*(uintptr_t *)opi->ret = opi->data;
Expand Down
1 change: 1 addition & 0 deletions include/vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ void vm_prepare(const VM *vm, m_bit*) __attribute__((hot));
ANN static inline void vm_run(const VM *vm) {
vm_prepare(vm, NULL);
}
void vm_force_run(const VM *vm);
ANN void vm_run_audio(const VM *vm);
ANEW VM * new_vm(MemPool, const bool);
ANN void vm_lock(VM const *);
Expand Down
1 change: 0 additions & 1 deletion opcode.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ HandleEffect~pc
PerformEffect
NoOp
DebugLine~u
DebugValue
DebugPush
DebugPop
EOC
Expand Down
26 changes: 13 additions & 13 deletions src/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
enum compile_type { COMPILE_NAME, COMPILE_MSTR, COMPILE_FILE };

struct Compiler {
const m_str base;
const char *base;
m_str name;
union {
m_str data;
FILE * file;
const char* data;
FILE * file;
};
Ast ast;
struct Vector_ args;
Expand Down Expand Up @@ -57,7 +57,7 @@ ANN static bool _compiler_open(struct Compiler *c) {
xfree(name);
return c->name ? !!(c->file = fopen(c->name, "r")) : false;
} else if (c->type == COMPILE_MSTR) {
c->file = c->data ? fmemopen(c->data, strlen(c->data), "r") : NULL;
c->file = c->data ? fmemopen((void*)c->data, strlen(c->data), "r") : NULL;
return !!c->file;
}
return true;
Expand Down Expand Up @@ -114,7 +114,7 @@ ANN static inline bool passes(struct Gwion_ *gwion, struct Compiler *c) {
load_context(ctx, env);
for(uint32_t i = 0; i < mp_vector_len(c->values); i++) {
const Value v = *mp_vector_at(c->values, Value, i);
set_vflag(v, vflag_builtin);
set_vflag(v, vflag_builtin); // TODO: we should copy the values, maybe
if(isa(v->type, gwion->type[et_class])) {
const Type t = (Type)v->d.ptr;
type_addref(t);
Expand Down Expand Up @@ -165,41 +165,41 @@ ANN static m_uint compile(struct Gwion_ *gwion, struct Compiler *c) {
return ret;
}

ANN m_uint compile_filename_values(struct Gwion_ *gwion, const m_str filename, MP_Vector *values) {
ANN m_uint compile_filename_values(struct Gwion_ *gwion, const char *filename, MP_Vector *values) {
struct Compiler c = {.base = filename, .type = COMPILE_NAME, .values=values};
return compile(gwion, &c);
}

ANN m_uint compile_string_values(struct Gwion_ *gwion, const m_str filename,
const m_str data, MP_Vector *values) {
ANN m_uint compile_string_values(struct Gwion_ *gwion, const char *filename,
const char *data, MP_Vector *values) {
struct Compiler c = {.base = filename, .type = COMPILE_MSTR, .data = data, .values=values};
return compile(gwion, &c);
}

ANN m_uint compile_file_values(struct Gwion_ *gwion, const m_str filename,
ANN m_uint compile_file_values(struct Gwion_ *gwion, const char *filename,
FILE *file, MP_Vector *values) {
struct Compiler c = {.base = filename, .type = COMPILE_FILE, .file = file, .values=values};
return compile(gwion, &c);
}

ANN m_uint compile_filename_xid_values(struct Gwion_ *gwion, const m_str filename,
ANN m_uint compile_filename_xid_values(struct Gwion_ *gwion, const char* filename,
const m_uint xid, MP_Vector *values) {
struct Compiler c = {.base = filename, .type = COMPILE_NAME, .values=values};
if (!compile(gwion, &c)) return 0;
assert(c.shred);
return c.shred->tick->xid = xid;
}

ANN m_uint compile_string_xid_values(struct Gwion_ *gwion, const m_str filename,
const m_str data, const m_uint xid, MP_Vector *values) {
ANN m_uint compile_string_xid_values(struct Gwion_ *gwion, const char *filename,
const char *data, const m_uint xid, MP_Vector *values) {
struct Compiler c = {.base = filename, .type = COMPILE_MSTR, .data = data, .values=values};
if (!compile(gwion, &c)) return 0;
assert(c.shred);
gwion->vm->shreduler->shred_ids--;
return c.shred->tick->xid = xid;
}

ANN m_uint compile_file_xid_values(struct Gwion_ *gwion, const m_str filename,
ANN m_uint compile_file_xid_values(struct Gwion_ *gwion, const char * filename,
FILE *file, const m_uint xid, MP_Vector *values) {
struct Compiler c = {.base = filename, .type = COMPILE_FILE, .file = file, .values=values};
if (!compile(gwion, &c)) return 0;
Expand Down
35 changes: 22 additions & 13 deletions src/emit/emit.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ static inline m_uint emit_push_global(const Emitter emit) {
return env_push_global(emit->env);
}

static inline void emit_debug(const Emitter emit, const Value v) {
if (!emit->info->debug) return;
const Instr instr = emit_add_instr(emit, DebugValue);
instr->m_val = (m_uint)v;
}

ANEW static Frame *new_frame(MemPool p) {
Frame *frame = mp_calloc(p, Frame);
Expand Down Expand Up @@ -260,7 +255,12 @@ ANN void emit_pop_scope(const Emitter emit) {
ANN void emit_push_scope(const Emitter emit) {
frame_push(emit->code->frame);
vector_add(&emit->info->pure, 0);
if (emit->info->debug) emit_add_instr(emit, DebugPush);
//if (emit->info->debug) emit_add_instr(emit, DebugPush);
if (emit->info->debug) {

const Instr instr = emit_add_instr(emit, DebugPush);
instr->m_val = emit->status.line;
}
}

ANN m_uint emit_code_offset(const Emitter emit) {
Expand Down Expand Up @@ -978,15 +978,22 @@ static INSTR(UsedBy) {
const MP_Vector *v =(MP_Vector*)instr->m_val;
for(uint32_t i = 0; i < v->len; i++) {
const Func f = *mp_vector_at(v, Func, i);
const Instr instr = (Instr)vector_front(&f->code->instr);
instr->m_val2++;
for(m_uint j = 0; i < vector_size(&f->code->instr); j++) {
const Instr instr = (Instr)vector_at(&f->code->instr, j);
if(instr->execute == FuncWait) {
instr->m_val2++;
break;
}
}
}
}

ANN static void used_by(const Emitter emit, const Value v) {
puts("emit used by");
MP_Vector *vec = new_mp_vector(emit->gwion->mp, Func, 0);
for(uint32_t i = 0; i < v->used_by->len; i++) {
const Func f = *mp_vector_at(v->used_by, Func, i);
if(f->_wait) puts("Adding to wait list");
if(f->_wait) mp_vector_add(emit->gwion->mp, &vec, Func, f);
}
free_mp_vector(emit->gwion->mp, Func, v->used_by);
Expand All @@ -1010,7 +1017,6 @@ ANN static bool emit_exp_decl_non_static(const Emitter emit,
else CHECK_B(emit_exp(emit, decl->args));
}
f_instr *exec = (f_instr *)allocmember;
if (!emit->env->scope->depth) emit_debug(emit, v);
if (!vflag(v, vflag_member)) {
if(v->used_by) used_by(emit, v);
v->from->offset = decl_non_static_offset(emit, decl, type);
Expand Down Expand Up @@ -1915,10 +1921,15 @@ DECL_EXP_FUNC(emit, bool, Emitter)
ANN2(1) /*static */ bool emit_exp(const Emitter emit, /* const */ Exp* e) {
Exp* exp = e;
do {
if (emit->info->debug && emit->status.line < e->loc.first.line) {
if (emit->info->debug){

printf("debug line: %i\n", e->loc.first.line);
if(emit->status.line < e->loc.first.line) {
const Instr instr = emit_add_instr(emit, DebugLine);
instr->m_val = emit->status.line = e->loc.first.line;
}
emit->status.line = e->loc.first.line;
}
CHECK_B(emit_exp_func[exp->exp_type](emit, &exp->d));
if (exp->cast_to) CHECK_B(emit_implicit_cast(emit, exp, exp->cast_to));
if (is_object(emit->gwion, exp->type) &&
Expand Down Expand Up @@ -2207,12 +2218,10 @@ ANN static bool _emit_stmt_each(const Emitter emit, const Stmt_Each stmt,
stmt->var.value->from->offset = val_offset;
//value_addref(stmt->v);
_nspc_add_value(emit->env->curr, stmt->var.tag.sym, stmt->var.value);
emit_debug(emit, stmt->var.value);
if (stmt->idx.tag.sym) {
stmt->idx.value->from->offset = key_offset;
_nspc_add_value(emit->env->curr, stmt->idx.tag.sym, stmt->var.value);
//value_addref(stmt->idx->v);
emit_debug(emit, stmt->idx.value);
}
struct Looper loop = {.exp = stmt->exp,
.stmt = stmt->body,
Expand Down Expand Up @@ -2606,6 +2615,7 @@ ANN static bool emit_exp_dot(const Emitter emit, const Exp_Dot *member) {
ANN static inline void emit_func_def_init(const Emitter emit, const Func func) {
emit_push_code(emit, func->name);
if(mp_vector_len(func->_wait)) {
puts("wait");
const Instr instr = emit_add_instr(emit, FuncWait);
instr->m_val = (m_uint) func;
}
Expand All @@ -2617,7 +2627,6 @@ ANN static void emit_func_def_args(const Emitter emit, Arg_List args) {
const Type type = arg->var.vd.value->type;
emit->code->stack_depth += type->size;
arg->var.vd.value->from->offset = emit_localn(emit, type);
emit_debug(emit, arg->var.vd.value);
_nspc_add_value(emit->env->curr, insert_symbol(arg->var.vd.value->name), arg->var.vd.value);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/emit/emitter.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ emit_add_instr(const Emitter emit, const f_instr f) {

ANN2(1) void emit_fast_except(const Emitter emit, const ValueFrom *vf, const loc_t loc) {
const Instr instr = emit_add_instr(emit, fast_except);
if(vf) {
if(!emit->info->debug && vf) {
struct FastExceptInfo *info = mp_malloc2(emit->gwion->mp, sizeof(struct FastExceptInfo));
info->file = emit->env->name;
info->loc = loc;
Expand Down
5 changes: 3 additions & 2 deletions src/emit/escape.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "emit.h"
#include "escape.h"

char escape_table[256] = {
static char escape_table[256] = {
['0'] = '0',
['\''] = '\'',
['"'] = '"',
Expand Down Expand Up @@ -63,7 +63,8 @@ bool escape_str(const Emitter emit, const m_str base, const loc_t loc) {
}
} else {
char out;
CHECK_B((*str++ = (char)get_escape(emit, (char)c, &out, loc)));
CHECK_B(get_escape(emit, (char)c, &out, loc));
*str++ = out;
}
} else
*str++ = (char)*str_lit;
Expand Down
19 changes: 16 additions & 3 deletions src/env/env_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,23 @@ ANN Type find_initial(const Env env, const Symbol xid) {
ANN Type find_type(const Env env, Type_Decl *td) {
DECL_O(Type, type, = find_initial(env, td->tag.sym));
while ((td = td->next) && type && type->nspc) {
const Nspc nspc = type->nspc;
if(!(type = find_in_parent(type, td->tag.sym)))
// const Nspc nspc = type->nspc;
// if(!(type = find_in_parent(type, td->tag.sym)))
//
//enERR_O(td->tag.loc, _("...(cannot find class '%s' in nspc '%s')"),
//s_name(td->tag.sym), nspc->name);


Type_Decl *next = td->next;
td->next = NULL;
env_push_type(env, type);
type = known_type(env, td);
if(!type)
ERR_O(td->tag.loc, _("...(cannot find class '%s' in nspc '%s')"),
s_name(td->tag.sym), nspc->name);
s_name(td->tag.sym), env->class_def->name);
env_pop(env, 0); // respect scope depth // use env scope
td->next = next;

}
return type;
}
Expand Down
1 change: 1 addition & 0 deletions src/env/func.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ ANN void builtin_func(const Gwion gwion, const Func f, void *func_ptr) {

ANN void print_signature(const Gwion gwion, const Func f) {
struct GwfmtState ls = {.minimize=true, .ppa = gwion->ppa};
// gwfmt_state_init(&ls);
text_init(&ls.text, gwion->mp);
Gwfmt l = {.mp = gwion->mp, .st = gwion->st, .ls = &ls, .line = 1, .last = cht_nl };
gwfmt_func_def(&l, f->def);
Expand Down
2 changes: 1 addition & 1 deletion src/import/import_cdef.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,5 @@ ANN bool gwi_class_end(const Gwi gwi) {
}
*/
env_pop(gwi->gwion->env, 0);
return false;
return true;
}
Loading

0 comments on commit 7e09034

Please sign in to comment.