Skip to content

Commit

Permalink
issue #100
Browse files Browse the repository at this point in the history
  • Loading branch information
dibyendumajumdar committed Oct 7, 2016
1 parent 860904f commit 8e19f4c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
7 changes: 7 additions & 0 deletions include/ravi_gccjit.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ typedef enum {
LUA__TNUMBER = LUA_TNUMBER,
LUA__TSTRING = ctb(LUA_TSTRING),
LUA__TTABLE = ctb(LUA_TTABLE),
RAVI__TLTABLE = ctb(LUA_TTABLE),
RAVI__TIARRAY = ctb(RAVI_TIARRAY),
RAVI__TFARRAY = ctb(RAVI_TFARRAY),
LUA__TFUNCTION = ctb(LUA_TFUNCTION),
LUA__TUSERDATA = ctb(LUA_TUSERDATA),
LUA__TTHREAD = ctb(LUA_TTHREAD),
Expand Down Expand Up @@ -513,6 +516,10 @@ extern gcc_jit_rvalue *
ravi_emit_is_not_value_of_type(ravi_function_def_t *def,
gcc_jit_rvalue *value_type, int lua_type);

extern gcc_jit_rvalue *
ravi_emit_is_not_value_of_type_class(ravi_function_def_t *def,
gcc_jit_rvalue *value_type, int lua_type);

extern void ravi_emit_store_reg_i_withtype(ravi_function_def_t *def,
gcc_jit_rvalue *ivalue,
gcc_jit_lvalue *reg);
Expand Down
14 changes: 14 additions & 0 deletions src/ravi_gcccodegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,20 @@ gcc_jit_rvalue *ravi_emit_is_not_value_of_type(ravi_function_def_t *def,
#endif
}

gcc_jit_rvalue *
ravi_emit_is_not_value_of_type_class(ravi_function_def_t *def,
gcc_jit_rvalue *value_type, int lua_type) {
gcc_jit_rvalue *bit_mask = ravi_int_constant(def, 0x0F);
gcc_jit_rvalue *novariant_type = gcc_jit_context_new_binary_op(
def->function_context, NULL, GCC_JIT_BINARY_OP_BITWISE_AND,
def->ravi->types->C_intT, value_type,
bit_mask);
return gcc_jit_context_new_unary_op(
def->function_context, NULL, GCC_JIT_UNARY_OP_LOGICAL_NEGATE,
def->ravi->types->C_boolT,
ravi_emit_is_value_of_type(def, novariant_type, lua_type));
}

/* Store an integer value and set type to TNUMINT */
void ravi_emit_store_reg_i_withtype(ravi_function_def_t *def,
gcc_jit_rvalue *ivalue,
Expand Down
39 changes: 17 additions & 22 deletions src/ravi_gcctable.c
Original file line number Diff line number Diff line change
Expand Up @@ -525,45 +525,40 @@ void ravi_emit_TOARRAY(ravi_function_def_t *def, int A, int array_type_expected,

// if (!ttistable(ra) || hvalue(ra)->ravi_array.type != RAVI_TARRAYINT)
// luaG_runerror(L, "integer[] expected");
OpCode op = OP_RAVI_TOTAB;
lua_typecode_t expected_type = RAVI__TLTABLE;
switch (array_type_expected) {
case RAVI_TARRAYINT:
op = OP_RAVI_TOARRAYI;
expected_type = RAVI__TIARRAY;
break;
case RAVI_TARRAYFLT:
op = OP_RAVI_TOARRAYF;
expected_type = RAVI__TFARRAY;
break;
case RAVI_TTABLE:
default: break;
}

ravi_emit_load_base(def);
gcc_jit_lvalue *ra = ravi_emit_get_register(def, A);
gcc_jit_lvalue *type = ravi_emit_load_type(def, ra);

// type != LUA_TTABLE ?
// type != expected_type ?
gcc_jit_rvalue *cmp1 = ravi_emit_is_not_value_of_type(
def, gcc_jit_lvalue_as_rvalue(type), LUA__TTABLE);
def, gcc_jit_lvalue_as_rvalue(type), expected_type);

gcc_jit_block *raise_error = gcc_jit_function_new_block(
def->jit_function, unique_name(def, "TOARRAY_if_not_table", pc));
gcc_jit_block *else1 = gcc_jit_function_new_block(
def->jit_function, unique_name(def, "TOARRAY_test_if_array", pc));
gcc_jit_block *done = gcc_jit_function_new_block(
def->jit_function, unique_name(def, "TOARRAY_done", pc));
ravi_emit_conditional_branch(def, cmp1, raise_error, else1);
ravi_emit_conditional_branch(def, cmp1, raise_error, done);
ravi_set_current_block(def, raise_error);

// Conversion failed, so raise error
ravi_emit_raise_lua_error(def, errmsg);
ravi_emit_branch(def, done);

ravi_set_current_block(def, else1);

// Get table
gcc_jit_rvalue *h = ravi_emit_load_reg_h(def, ra);
// Get array type
gcc_jit_rvalue *ravi_array_type =
gcc_jit_lvalue_as_rvalue(ravi_emit_load_ravi_arraytype(def, h));

// array_type == RAVI_TARRAYXXX?
gcc_jit_rvalue *expected = gcc_jit_context_new_rvalue_from_int(
def->function_context, def->ravi->types->lu_byteT, array_type_expected);
gcc_jit_rvalue *cmp2 = ravi_emit_comparison(def, GCC_JIT_COMPARISON_EQ,
ravi_array_type, expected);

// If array then fine else raise error
ravi_emit_conditional_branch(def, cmp2, done, raise_error);

ravi_set_current_block(def, done);
}

Expand Down

0 comments on commit 8e19f4c

Please sign in to comment.