1
0
mirror of https://github.com/janet-lang/janet synced 2025-07-07 20:42:54 +00:00
This commit is contained in:
Calvin Rose 2025-04-11 22:44:36 -05:00
parent 3cbdf26aa2
commit c9305a0a42
2 changed files with 49 additions and 50 deletions

View File

@ -1087,47 +1087,44 @@ static int check_const_valid(JanetSysIR *sysir, Janet constant, uint32_t t) {
switch (janet_type(constant)) { switch (janet_type(constant)) {
default: default:
return 0; return 0;
case JANET_TUPLE: case JANET_TUPLE: {
{ const Janet *elements = janet_unwrap_tuple(constant);
const Janet *elements = janet_unwrap_tuple(constant); int32_t len = janet_tuple_length(elements);
int32_t len = janet_tuple_length(elements); if (p != JANET_PRIM_ARRAY) return 0;
if (p != JANET_PRIM_ARRAY) return 0; if ((uint64_t) len != tinfo->array.fixed_count) return 0;
if ((uint64_t) len != tinfo->array.fixed_count) return 0; for (int32_t i = 0; i < len; i++) {
for (int32_t i = 0; i < len; i++) { if (!check_const_valid(sysir, elements[i], tinfo->array.type)) return 0;
if (!check_const_valid(sysir, elements[i], tinfo->array.type)) return 0;
}
return 1;
} }
return 1;
}
case JANET_BOOLEAN: case JANET_BOOLEAN:
return p == JANET_PRIM_BOOLEAN; return p == JANET_PRIM_BOOLEAN;
case JANET_STRING: case JANET_STRING:
case JANET_SYMBOL: case JANET_SYMBOL:
case JANET_POINTER: case JANET_POINTER:
return p == JANET_PRIM_POINTER; return p == JANET_PRIM_POINTER;
case JANET_NUMBER: case JANET_NUMBER: {
{ double x = janet_unwrap_number(constant);
double x = janet_unwrap_number(constant); if (p == JANET_PRIM_F64) return 1;
if (p == JANET_PRIM_F64) return 1; if (p == JANET_PRIM_F32) return 1;
if (p == JANET_PRIM_F32) return 1; if (x != floor(x)) return 0; /* Filter out non-integers */
if (x != floor(x)) return 0; /* Filter out non-integers */ if (p == JANET_PRIM_U8 && (x >= 0 && x <= UINT8_MAX)) return 1;
if (p == JANET_PRIM_U8 && (x >= 0 && x <= UINT8_MAX)) return 1; if (p == JANET_PRIM_S8 && (x >= INT8_MIN && x <= INT8_MAX)) return 1;
if (p == JANET_PRIM_S8 && (x >= INT8_MIN && x <= INT8_MAX)) return 1; if (p == JANET_PRIM_U16 && (x >= 0 && x <= UINT16_MAX)) return 1;
if (p == JANET_PRIM_U16 && (x >= 0 && x <= UINT16_MAX)) return 1; if (p == JANET_PRIM_S16 && (x >= INT16_MIN && x <= INT16_MAX)) return 1;
if (p == JANET_PRIM_S16 && (x >= INT16_MIN && x <= INT16_MAX)) return 1; if (p == JANET_PRIM_U32 && (x >= 0 && x <= UINT32_MAX)) return 1;
if (p == JANET_PRIM_U32 && (x >= 0 && x <= UINT32_MAX)) return 1; if (p == JANET_PRIM_S32 && (x >= INT32_MIN && x <= INT32_MAX)) return 1;
if (p == JANET_PRIM_S32 && (x >= INT32_MIN && x <= INT32_MAX)) return 1; if (p == JANET_PRIM_U64 && (x >= 0 && x <= UINT64_MAX)) return 1;
if (p == JANET_PRIM_U64 && (x >= 0 && x <= UINT64_MAX)) return 1; if (p == JANET_PRIM_S64 && (x >= INT64_MIN && x <= INT64_MAX)) return 1;
if (p == JANET_PRIM_S64 && (x >= INT64_MIN && x <= INT64_MAX)) return 1; return 0;
return 0; }
} case JANET_ABSTRACT: {
case JANET_ABSTRACT: void *point = janet_unwrap_abstract(constant);
{ const JanetAbstractType *at = janet_abstract_type(point);
void *point = janet_unwrap_abstract(constant); if (at == &janet_s64_type && p == JANET_PRIM_S64) return 1;
const JanetAbstractType *at = janet_abstract_type(point); if (at == &janet_u64_type && p == JANET_PRIM_U64) return 1;
if (at == &janet_s64_type && p == JANET_PRIM_S64) return 1; return 0;
if (at == &janet_u64_type && p == JANET_PRIM_U64) return 1; }
return 0;
}
} }
} }
@ -1421,8 +1418,8 @@ static uint32_t janet_sysir_findprim(JanetSysIRLinkage *linkage, JanetPrim prim,
td.prim = prim; td.prim = prim;
janet_v_push(linkage->type_defs, td); janet_v_push(linkage->type_defs, td);
janet_table_put(linkage->type_name_lookup, janet_table_put(linkage->type_name_lookup,
janet_csymbolv(type_name), janet_csymbolv(type_name),
janet_wrap_number(linkage->type_def_count)); janet_wrap_number(linkage->type_def_count));
janet_v_push(linkage->type_names, janet_csymbol(type_name)); janet_v_push(linkage->type_names, janet_csymbol(type_name));
return linkage->type_def_count++; return linkage->type_def_count++;
} }
@ -1443,8 +1440,8 @@ static uint32_t janet_sysir_findpointer(JanetSysIRLinkage *linkage, uint32_t to,
td.pointer.type = to; td.pointer.type = to;
janet_v_push(linkage->type_defs, td); janet_v_push(linkage->type_defs, td);
janet_table_put(linkage->type_name_lookup, janet_table_put(linkage->type_name_lookup,
janet_csymbolv(type_name), janet_csymbolv(type_name),
janet_wrap_number(linkage->type_def_count)); janet_wrap_number(linkage->type_def_count));
janet_v_push(linkage->type_names, janet_csymbol(type_name)); janet_v_push(linkage->type_names, janet_csymbol(type_name));
return linkage->type_def_count++; return linkage->type_def_count++;
} }

View File

@ -234,7 +234,7 @@ void assign_registers(JanetSysx64Context *ctx) {
janet_panic("cannot assign registers for calling convention"); janet_panic("cannot assign registers for calling convention");
} }
} else if (assigned < 0xFFFF) { } else if (assigned < 0xFFFF) {
//} else if (assigned < 1) { //} else if (assigned < 1) {
/* Assign to register */ /* Assign to register */
uint32_t to = 0; uint32_t to = 0;
while ((1 << to) & assigned) to++; while ((1 << to) & assigned) to++;
@ -311,13 +311,13 @@ static void i_chunk(JanetSysx64Context *C, InstrChunk c) {
/* Emit one x86_64 instruction given all of the individual components */ /* Emit one x86_64 instruction given all of the individual components */
static void i_combine(JanetSysx64Context *C, static void i_combine(JanetSysx64Context *C,
InstrChunk prefix, InstrChunk prefix,
uint16_t opcode, uint16_t opcode,
InstrChunk mod_reg_rm, InstrChunk mod_reg_rm,
InstrChunk scaled_index, InstrChunk scaled_index,
InstrChunk displacement, InstrChunk displacement,
InstrChunk immediate, InstrChunk immediate,
const char *msg) { const char *msg) {
assert(mod_reg_rm.bytes < 3); assert(mod_reg_rm.bytes < 3);
assert(scaled_index.bytes < 2); assert(scaled_index.bytes < 2);
assert(opcode < 512); assert(opcode < 512);
@ -363,12 +363,14 @@ static void e_mov_to_reg(JanetSysx64Context *ctx, x64Reg d, x64Reg s, MoveMode m
return; return;
} }
if (mm == MOV_LOAD || s.storage != JANET_SYSREG_REGISTER) { if (mm == MOV_LOAD || s.storage != JANET_SYSREG_REGISTER) {
x64Reg t = d; d = s; s = t; /* swap */ x64Reg t = d;
d = s;
s = t; /* swap */
flip = 1; flip = 1;
} }
assert(s.storage == JANET_SYSREG_REGISTER); assert(s.storage == JANET_SYSREG_REGISTER);
opcode = 0x88; opcode = 0x88;
mod_rm |= (uint8_t) (s.index & 7) << 3; mod_rm |= (uint8_t)(s.index & 7) << 3;
if (s.index >= 8) rex |= REX_R; if (s.index >= 8) rex |= REX_R;
if (s.kind >= JANET_SYSREG_64) rex |= REX_W; if (s.kind >= JANET_SYSREG_64) rex |= REX_W;
if (d.storage == JANET_SYSREG_REGISTER) { if (d.storage == JANET_SYSREG_REGISTER) {
@ -376,7 +378,7 @@ static void e_mov_to_reg(JanetSysx64Context *ctx, x64Reg d, x64Reg s, MoveMode m
if (mm == MOV_FLAT) { if (mm == MOV_FLAT) {
mod_rm |= 0xC0u; /* mod = b11, reg, reg mode */ mod_rm |= 0xC0u; /* mod = b11, reg, reg mode */
} }
mod_rm |= (uint8_t) (d.index & 7); mod_rm |= (uint8_t)(d.index & 7);
} else { } else {
assert(mm == MOV_FLAT); assert(mm == MOV_FLAT);
/* d is memory */ /* d is memory */