Skip to content

Commit

Permalink
fix bug in VM_STD_VALUE_NUMBER; implement tonumber
Browse files Browse the repository at this point in the history
Signed-off-by: Shaw Summa <shawsumma@gmail.com>
  • Loading branch information
ShawSumma committed Apr 3, 2024
1 parent a908c4e commit 952978f
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 69 deletions.
56 changes: 55 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,60 @@
{
"files.associations": {
"*.c": "c",
"*.h": "c"
"*.h": "c",
"vector": "c",
"string_view": "c",
"sstream": "c",
"array": "c",
"atomic": "c",
"bit": "c",
"*.tcc": "c",
"cctype": "c",
"cinttypes": "c",
"clocale": "c",
"cmath": "c",
"compare": "c",
"concepts": "c",
"cstdarg": "c",
"cstddef": "c",
"cstdint": "c",
"cstdio": "c",
"cstdlib": "c",
"cstring": "c",
"ctime": "c",
"cwchar": "c",
"cwctype": "c",
"deque": "c",
"list": "c",
"map": "c",
"set": "c",
"string": "c",
"unordered_map": "c",
"unordered_set": "c",
"exception": "c",
"algorithm": "c",
"functional": "c",
"iterator": "c",
"memory": "c",
"memory_resource": "c",
"numeric": "c",
"optional": "c",
"random": "c",
"system_error": "c",
"tuple": "c",
"type_traits": "c",
"utility": "c",
"initializer_list": "c",
"iosfwd": "c",
"iostream": "c",
"istream": "c",
"limits": "c",
"new": "c",
"numbers": "c",
"ostream": "c",
"stdexcept": "c",
"streambuf": "c",
"typeinfo": "c",
"variant": "c"
}
}
25 changes: 16 additions & 9 deletions main/js/all.mjs
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,29 @@ const cc = `clang`;
const ld = `wasm-ld`;

const cFlagsList = [
'-target wasm32-unknown-unknown',
'-fignore-exceptions',
'-fPIC',
'-fvisibility=default',
'-cc1',
'-triple',
'wasm32-unknown-unknown',
'-emit-obj',
'-disable-llvm-verifier',
'-discard-value-names',
'-mframe-pointer=none',
'-Os',
'-w',
'-vectorize-loops',
'-vectorize-slp',
'-x',
'c',
];
const ldFlagsList = [
'--no-whole-archive',
'--import-undefined',
'--import-memory',
'--strip-debug',
'--export-dynamic',
'--experimental-pic',
'-shared',
];

const cflags = cFlagsList.join(' ');
const cFlags = cFlagsList.join(' ');
const ldflags = ldFlagsList.join(' ');

export const run = (args, opts) => {
Expand Down Expand Up @@ -56,7 +63,7 @@ export const run = (args, opts) => {
const outFile = `.minivm-cache/out${n}.wasm`;
const cSrc = mod.FS.readFile(`/in${n}.c`);
writeFileSync(inFile, cSrc);
execSync(`${cc} -O2 -c -w ${cflags} ${inFile} -o ${midFile}`);
execSync(`${cc} ${cFlags} ${inFile} -o ${midFile}`);
execSync(`${ld} ${ldflags} ${midFile} -o ${outFile}`)
const wasm = readFileSync(outFile);
unlinkSync(inFile);
Expand All @@ -80,7 +87,7 @@ export const config = {

stdin() {
const buf = Buffer.alloc(1);
readSync(0, buf, 0, 1);
readSync(0, buf, 0, 1, null);
return new Uint8Array(buf)[0];
},
};
Expand Down
22 changes: 13 additions & 9 deletions main/minivm.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ int main(int argc, char **argv) {
bool echo = false;
bool isrepl = true;
vm_table_t *std = vm_std_new(config);
for (int i = 1; i < argc; i++) {
char *arg = argv[i];
int i = 1;
while (i < argc) {
char *arg = argv[i++];
if (!strcmp(arg, "--")) {
break;
}
Expand Down Expand Up @@ -167,19 +168,18 @@ int main(int argc, char **argv) {

clock_t start = clock();

const char *name;
const char *src;
// if (!strcmp(arg, "-f")) {
// isrepl = last_isrepl;
// arg = argv[i + 1];
// i += 1;
// }
if (!strcmp(arg, "-e")) {
src = argv[i + 1];
i += 1;
src = argv[i++];
name = "<expr>";
} else {
src = vm_io_read(arg);
name = arg;
}

vm_std_set_arg(config, std, argv[0], name, argc - i, &argv[i]);

if (src == NULL) {
fprintf(stderr, "error: no such file: %s\n", arg);
}
Expand Down Expand Up @@ -218,10 +218,14 @@ int main(int argc, char **argv) {
double diff = (double)(end - start) / CLOCKS_PER_SEC * 1000;
printf("took: %.3fms\n", diff);
}

break;
}
}

if (isrepl) {
vm_std_set_arg(config, std, argv[0], "<repl>", argc - i, argv + i);

vm_lang_lua_repl(config, std, blocks);
}

Expand Down
18 changes: 18 additions & 0 deletions main/test/globals.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

for k,v in pairs(_G) do
if type(v) == 'function' then
print(k)
end
end

for k,v in pairs(_G) do
if k ~= '_G' then
if type(v) == 'table' then
for l, w in pairs(v) do
if type(w) == 'function' then
print(k .. '.' .. l)
end
end
end
end
end
4 changes: 4 additions & 0 deletions test/basic/arg.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

for i=1, #arg do
print('arg[' .. tostring(i) .. '] = ' .. arg[i])
end
17 changes: 0 additions & 17 deletions test/basic/args.lua

This file was deleted.

10 changes: 10 additions & 0 deletions test/fib/fib15.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

local function fib(n)
if n < 2 then
return n
else
return fib(n-1) + fib(n-2)
end
end

print(fib(15))
4 changes: 2 additions & 2 deletions vm/backend/tb_ver.h
Original file line number Diff line number Diff line change
Expand Up @@ -1925,11 +1925,11 @@ static void *vm_tb_ver_rfunc_comp(vm_rblock_t *rblock) {
} else if (state->config->target == VM_TARGET_TB) {
TB_FeatureSet features = (TB_FeatureSet){TB_FEATURE_FRAME_PTR};
if (VM_USE_DUMP && state->config->dump_asm) {
TB_FunctionOutput *out = tb_codegen(f, state->worklist, state->tmp_arena, state->code_arena, &features, true);
TB_FunctionOutput *out = tb_codegen(f, state->worklist, state->ir_arena, state->tmp_arena, state->code_arena, &features, true);
fprintf(stdout, "\n--- x86asm ---\n");
tb_output_print_asm(out, stdout);
} else {
tb_codegen(f, state->worklist, state->tmp_arena, state->code_arena, &features, false);
tb_codegen(f, state->worklist, state->ir_arena, state->tmp_arena, state->code_arena, &features, false);
}

#if VM_USE_DUMP
Expand Down
8 changes: 8 additions & 0 deletions vm/obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,27 +96,35 @@ void vm_table_get_pair(vm_table_t *table, vm_pair_t *pair);
}, \
}

#define VM_STD_VALUE_NIL ((vm_std_value_t) {.tag = (VM_TYPE_NIL)})

#define VM_STD_VALUE_NUMBER(CONFIG_, VALUE_) ({ \
vm_config_t *config_ = (CONFIG_); \
vm_std_value_t ret_; \
switch (config_->use_num) { \
case VM_USE_NUM_I8: { \
ret_ = VM_STD_VALUE_LITERAL(i8, (int8_t)(VALUE_)); \
break; \
} \
case VM_USE_NUM_I16: { \
ret_ = VM_STD_VALUE_LITERAL(i16, (int16_t)(VALUE_)); \
break; \
} \
case VM_USE_NUM_I32: { \
ret_ = VM_STD_VALUE_LITERAL(i32, (int32_t)(VALUE_)); \
break; \
} \
case VM_USE_NUM_I64: { \
ret_ = VM_STD_VALUE_LITERAL(i64, (int64_t)(VALUE_)); \
break; \
} \
case VM_USE_NUM_F32: { \
ret_ = VM_STD_VALUE_LITERAL(f32, (float)(VALUE_)); \
break; \
} \
case VM_USE_NUM_F64: { \
ret_ = VM_STD_VALUE_LITERAL(f64, (double)(VALUE_)); \
break; \
} \
} \
ret_; \
Expand Down
Loading

0 comments on commit 952978f

Please sign in to comment.