Skip to content
Myr

Error on bytecodes longer than 0xFFFF (fix #269)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

This lets us use uint16_t for the program counter. JVM languages have the same limit on method size so it is a reasonable limit.

authored by David Tolnay · committed by David Tolnay · parent 8295baf · Browse files · 3 files+13 added−7 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+13 added−7 removed

  • other:+13 added−7 removed

3 files+13 added−7 removed

modified:compile.c+11 added-5 removed
1045 unmodified lines
1046 *b = ret;1047 return errors;1048}10491050static int compile(struct bytecode* bc, block b) {1050static int compile(struct bytecode* bc, block b, struct locfile* lf) {1051 int errors = 0;1052 int pos = 0;1053 int var_frame_idx = 0;1054 bc->nsubfunctions = 0;
32 unmodified lines
1087 bc->globals->cfunctions[idx] = *curr->imm.cfunc;1088 curr->imm.intval = idx;1089 }1090 }1091 if (pos > 0xFFFF) {1092 // too long for program counter to fit in uint16_t1093 locfile_locate(lf, UNKNOWN_LOCATION,1094 "function compiled to %d bytes which is too long", pos);1095 errors++;1096 }1097 bc->codelen = pos;1098 bc->debuginfo = jv_object_set(bc->debuginfo, jv_string("locals"), localnames);1099 if (bc->nsubfunctions) {1100 bc->subfunctions = jv_mem_alloc(sizeof(struct bytecode*) * bc->nsubfunctions);1101 for (inst* curr = b.first; curr; curr = curr->next) {
12 unmodified lines
1114 param->compiled = subfn;1115 params = jv_array_append(params, jv_string(param->symbol));1116 }1117 subfn->debuginfo = jv_object_set(subfn->debuginfo, jv_string("params"), params);1111 errors += compile(subfn, curr->subfn);1118 errors += compile(subfn, curr->subfn, lf);1119 curr->subfn = gen_noop();1120 }1121 }1122 } else {1123 bc->subfunctions = 0;1124 }1118 bc->codelen = pos;1125 uint16_t* code = jv_mem_alloc(sizeof(uint16_t) * bc->codelen);1126 bc->code = code;1127 pos = 0;1128 jv constant_pool = jv_array();
50 unmodified lines
1179 block_free(b);1180 return errors;1181}11821177int block_compile(block b, struct bytecode** out) {1183int block_compile(block b, struct bytecode** out, struct locfile* lf) {1184 struct bytecode* bc = jv_mem_alloc(sizeof(struct bytecode));1185 bc->parent = 0;1186 bc->nclosures = 0;1187 bc->globals = jv_mem_alloc(sizeof(struct symbol_table));
1 unmodified line
1189 bc->globals->ncfunctions = 0;1190 bc->globals->cfunctions = jv_mem_alloc(sizeof(struct cfunction) * ncfunc);1191 bc->globals->cfunc_names = jv_array();1192 bc->debuginfo = jv_object_set(jv_object(), jv_string("name"), jv_null());1187 int nerrors = compile(bc, b);1193 int nerrors = compile(bc, b, lf);1194 assert(bc->globals->ncfunctions == ncfunc);1195 if (nerrors > 0) {1196 bytecode_free(bc);1197 *out = 0;
13 unmodified lines
modified:compile.h+1 added-1 removed
73 unmodified lines
74block block_drop_unreferenced(block body);7576jv block_take_imports(block* body);7778int block_compile(block, struct bytecode**);78int block_compile(block, struct bytecode**, struct locfile*);7980void block_free(block);8182
19 unmodified lines
modified:execute.c+1 added-1 removed
1086 unmodified lines
1087 }10881089 nerrors = builtins_bind(jq, &program);1090 if (nerrors == 0) {1091 nerrors = block_compile(program, &jq->bc);1091 nerrors = block_compile(program, &jq->bc, locations);1092 }1093 }1094 if (nerrors)1095 jq_report_error(jq, jv_string_fmt("jq: %d compile %s", nerrors, nerrors > 1 ? "errors" : "error"));
60 unmodified lines