Skip to content

Commit

Permalink
Removed e_instruction__BOOLEAN.
Browse files Browse the repository at this point in the history
It has been unified into e_instruction__INSTANCE.
  • Loading branch information
shin1m committed Jul 5, 2024
1 parent c99c80d commit 496317f
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 91 deletions.
13 changes: 8 additions & 5 deletions include/xemmai/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,14 @@ struct t_literal : t_node
virtual t_operand f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_clear);
};

template<>
struct t_literal<bool> : t_literal<t_object*>
{
t_literal(const t_at& a_at, bool a_value) : t_literal<t_object*>(a_at, reinterpret_cast<t_object*>(a_value ? e_tag__TRUE : e_tag__FALSE))
{
}
};

struct t_unary : t_node
{
t_instruction v_instruction;
Expand Down Expand Up @@ -624,11 +632,6 @@ struct XEMMAI__LOCAL t_emit
}
};

template<>
constexpr t_instruction t_emit::f_instruction_of<bool>()
{
return e_instruction__BOOLEAN;
}
template<>
constexpr t_instruction t_emit::f_instruction_of<intptr_t>()
{
Expand Down
2 changes: 0 additions & 2 deletions include/xemmai/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,10 @@ enum t_instruction
e_instruction__CLASS,
e_instruction__SUPER,
e_instruction__NUL,
e_instruction__BOOLEAN,
e_instruction__INTEGER,
e_instruction__FLOAT,
e_instruction__INSTANCE,
e_instruction__RETURN_NUL,
e_instruction__RETURN_BOOLEAN,
e_instruction__RETURN_INTEGER,
e_instruction__RETURN_FLOAT,
e_instruction__RETURN_INSTANCE,
Expand Down
127 changes: 59 additions & 68 deletions src/ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -717,19 +717,10 @@ t_operand t_scope_put::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool
a_emit << e_instruction__FLOAT << v_variable.v_index << operand.v_float;
break;
case t_operand::e_tag__LITERAL:
switch (reinterpret_cast<uintptr_t>(operand.v_value)) {
case e_tag__NULL:
a_emit << e_instruction__NUL << v_variable.v_index;
break;
case e_tag__FALSE:
a_emit << e_instruction__BOOLEAN << v_variable.v_index << 0;
break;
case e_tag__TRUE:
a_emit << e_instruction__BOOLEAN << v_variable.v_index << 1;
break;
default:
if (operand.v_value)
a_emit << e_instruction__INSTANCE << v_variable.v_index << operand.v_value;
}
else
a_emit << e_instruction__NUL << v_variable.v_index;
break;
case t_operand::e_tag__VARIABLE:
a_emit << e_instruction__STACK_PUT << operand.v_index << v_variable.v_index;
Expand Down Expand Up @@ -832,25 +823,25 @@ t_operand t_unary::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_cl
a_emit.f_pop();
switch (v_instruction) {
case e_instruction__PLUS_T:
return t_literal<intptr_t>(v_at, operand.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, operand.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__MINUS_T:
return t_literal<intptr_t>(v_at, -operand.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, -operand.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__NOT_T:
return t_literal<bool>(v_at, false).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, false).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__COMPLEMENT_T:
return t_literal<intptr_t>(v_at, ~operand.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, ~operand.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
default:
assert(false);
}
} else if (operand.v_tag == t_operand::e_tag__FLOAT) {
a_emit.f_pop();
switch (v_instruction) {
case e_instruction__PLUS_T:
return t_literal<double>(v_at, operand.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, operand.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__MINUS_T:
return t_literal<double>(v_at, -operand.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, -operand.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__NOT_T:
return t_literal<bool>(v_at, false).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, false).f_emit(a_emit, a_tail, a_operand, a_clear);
default:
f_throw(L"not supported."sv);
}
Expand Down Expand Up @@ -901,69 +892,69 @@ t_operand t_binary::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_c
a_emit.f_pop();
switch (v_instruction) {
case e_instruction__MULTIPLY_TT:
return t_literal<intptr_t>(v_at, left.v_integer * right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer * right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__DIVIDE_TT:
return t_literal<intptr_t>(v_at, left.v_integer / right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer / right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__MODULUS_TT:
return t_literal<intptr_t>(v_at, left.v_integer % right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer % right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__ADD_TT:
return t_literal<intptr_t>(v_at, left.v_integer + right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer + right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__SUBTRACT_TT:
return t_literal<intptr_t>(v_at, left.v_integer - right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer - right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__LEFT_SHIFT_TT:
return t_literal<intptr_t>(v_at, left.v_integer << right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer << right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__RIGHT_SHIFT_TT:
return t_literal<intptr_t>(v_at, static_cast<uintptr_t>(left.v_integer) >> right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__LESS_TT:
return t_literal<bool>(v_at, left.v_integer < right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer < right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__LESS_EQUAL_TT:
return t_literal<bool>(v_at, left.v_integer <= right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer <= right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__GREATER_TT:
return t_literal<bool>(v_at, left.v_integer > right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer > right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__GREATER_EQUAL_TT:
return t_literal<bool>(v_at, left.v_integer >= right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer >= right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__EQUALS_TT:
case e_instruction__IDENTICAL_TT:
return t_literal<bool>(v_at, left.v_integer == right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer == right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__NOT_EQUALS_TT:
case e_instruction__NOT_IDENTICAL_TT:
return t_literal<bool>(v_at, left.v_integer != right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer != right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__AND_TT:
return t_literal<intptr_t>(v_at, left.v_integer & right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer & right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__XOR_TT:
return t_literal<intptr_t>(v_at, left.v_integer ^ right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer ^ right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__OR_TT:
return t_literal<intptr_t>(v_at, left.v_integer | right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer | right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
default:
assert(false);
}
} else if (right.v_tag == t_operand::e_tag__FLOAT) {
a_emit.f_pop();
switch (v_instruction) {
case e_instruction__MULTIPLY_TT:
return t_literal<double>(v_at, left.v_integer * right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer * right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__DIVIDE_TT:
return t_literal<double>(v_at, left.v_integer / right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer / right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__ADD_TT:
return t_literal<double>(v_at, left.v_integer + right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer + right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__SUBTRACT_TT:
return t_literal<double>(v_at, left.v_integer - right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer - right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__LESS_TT:
return t_literal<bool>(v_at, left.v_integer < right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer < right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__LESS_EQUAL_TT:
return t_literal<bool>(v_at, left.v_integer <= right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer <= right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__GREATER_TT:
return t_literal<bool>(v_at, left.v_integer > right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer > right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__GREATER_EQUAL_TT:
return t_literal<bool>(v_at, left.v_integer >= right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer >= right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__EQUALS_TT:
return t_literal<bool>(v_at, left.v_integer == right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer == right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__NOT_EQUALS_TT:
return t_literal<bool>(v_at, left.v_integer != right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_integer != right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__IDENTICAL_TT:
return t_literal<bool>(v_at, false).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, false).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__NOT_IDENTICAL_TT:
return t_literal<bool>(v_at, true).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, true).f_emit(a_emit, a_tail, a_operand, a_clear);
default:
f_throw(L"not supported."sv);
}
Expand All @@ -973,57 +964,57 @@ t_operand t_binary::f_emit(t_emit& a_emit, bool a_tail, bool a_operand, bool a_c
a_emit.f_pop();
switch (v_instruction) {
case e_instruction__MULTIPLY_TT:
return t_literal<double>(v_at, left.v_float * right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float * right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__DIVIDE_TT:
return t_literal<double>(v_at, left.v_float / right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float / right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__ADD_TT:
return t_literal<double>(v_at, left.v_float + right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float + right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__SUBTRACT_TT:
return t_literal<double>(v_at, left.v_float - right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float - right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__LESS_TT:
return t_literal<bool>(v_at, left.v_float < right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float < right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__LESS_EQUAL_TT:
return t_literal<bool>(v_at, left.v_float <= right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float <= right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__GREATER_TT:
return t_literal<bool>(v_at, left.v_float > right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float > right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__GREATER_EQUAL_TT:
return t_literal<bool>(v_at, left.v_float >= right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float >= right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__EQUALS_TT:
return t_literal<bool>(v_at, left.v_float == right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float == right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__NOT_EQUALS_TT:
return t_literal<bool>(v_at, left.v_float != right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float != right.v_integer).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__IDENTICAL_TT:
return t_literal<bool>(v_at, false).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, false).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__NOT_IDENTICAL_TT:
return t_literal<bool>(v_at, true).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, true).f_emit(a_emit, a_tail, a_operand, a_clear);
default:
f_throw(L"not supported."sv);
}
} else if (right.v_tag == t_operand::e_tag__FLOAT) {
a_emit.f_pop();
switch (v_instruction) {
case e_instruction__MULTIPLY_TT:
return t_literal<double>(v_at, left.v_float * right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float * right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__DIVIDE_TT:
return t_literal<double>(v_at, left.v_float / right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float / right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__ADD_TT:
return t_literal<double>(v_at, left.v_float + right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float + right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__SUBTRACT_TT:
return t_literal<double>(v_at, left.v_float - right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float - right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__LESS_TT:
return t_literal<bool>(v_at, left.v_float < right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float < right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__LESS_EQUAL_TT:
return t_literal<bool>(v_at, left.v_float <= right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float <= right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__GREATER_TT:
return t_literal<bool>(v_at, left.v_float > right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float > right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__GREATER_EQUAL_TT:
return t_literal<bool>(v_at, left.v_float >= right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float >= right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__EQUALS_TT:
case e_instruction__IDENTICAL_TT:
return t_literal<bool>(v_at, left.v_float == right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float == right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
case e_instruction__NOT_EQUALS_TT:
case e_instruction__NOT_IDENTICAL_TT:
return t_literal<bool>(v_at, left.v_float != right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
return t_literal(v_at, left.v_float != right.v_float).f_emit(a_emit, a_tail, a_operand, a_clear);
default:
f_throw(L"not supported."sv);
}
Expand Down
13 changes: 0 additions & 13 deletions src/code.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,10 @@ size_t t_code::f_loop(t_context* a_context, const void*** a_labels)
&&label__CLASS,
&&label__SUPER,
&&label__NUL,
&&label__BOOLEAN,
&&label__INTEGER,
&&label__FLOAT,
&&label__INSTANCE,
&&label__RETURN_NUL,
&&label__RETURN_BOOLEAN,
&&label__RETURN_INTEGER,
&&label__RETURN_FLOAT,
&&label__RETURN_INSTANCE,
Expand Down Expand Up @@ -451,14 +449,6 @@ size_t t_code::f_loop(t_context* a_context)
stack[0] = nullptr;
}
XEMMAI__CODE__BREAK
XEMMAI__CODE__CASE(BOOLEAN)
{
auto stack = base + reinterpret_cast<size_t>(*++pc);
auto value = reinterpret_cast<intptr_t>(*++pc) != 0;
++pc;
stack[0] = value;
}
XEMMAI__CODE__BREAK
XEMMAI__CODE__CASE(INTEGER)
{
auto stack = base + reinterpret_cast<size_t>(*++pc);
Expand Down Expand Up @@ -493,9 +483,6 @@ size_t t_code::f_loop(t_context* a_context)
XEMMAI__CODE__CASE(RETURN_NUL)
a_context->f_return(nullptr);
return -1;
XEMMAI__CODE__CASE(RETURN_BOOLEAN)
a_context->f_return(reinterpret_cast<intptr_t>(*++pc) != 0);
return -1;
XEMMAI__CODE__CASE(RETURN_INTEGER)
a_context->f_return(reinterpret_cast<intptr_t>(*++pc));
return -1;
Expand Down
6 changes: 3 additions & 3 deletions src/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -409,21 +409,21 @@ std::unique_ptr<ast::t_node> t_parser::f_unary(bool a_assignable)
{
auto value = v_lexer.f_integer();
v_lexer.f_next();
return f_action(indent, new ast::t_literal<intptr_t>(at, instruction == e_instruction__MINUS_T ? -value : value), a_assignable);
return f_action(indent, new ast::t_literal(at, instruction == e_instruction__MINUS_T ? -value : value), a_assignable);
}
case t_lexer::e_token__FLOAT:
{
auto value = v_lexer.f_float();
v_lexer.f_next();
return f_action(indent, new ast::t_literal<double>(at, instruction == e_instruction__MINUS_T ? -value : value), a_assignable);
return f_action(indent, new ast::t_literal(at, instruction == e_instruction__MINUS_T ? -value : value), a_assignable);
}
}
break;
case e_instruction__COMPLEMENT_T:
if (v_lexer.f_token() == t_lexer::e_token__INTEGER) {
auto value = v_lexer.f_integer();
v_lexer.f_next();
return f_action(indent, new ast::t_literal<intptr_t>(at, ~value), a_assignable);
return f_action(indent, new ast::t_literal(at, ~value), a_assignable);
}
break;
}
Expand Down

0 comments on commit 496317f

Please sign in to comment.