2021-12-21 01:18:22 +01:00
|
|
|
#include "runtime.h"
|
|
|
|
|
|
|
|
#include "util.h"
|
|
|
|
|
2021-12-23 20:10:02 +01:00
|
|
|
Value eval_binary(IRInstr instr, const Value *lhs, const Value *rhs) {
|
2021-12-21 01:18:22 +01:00
|
|
|
switch (instr) {
|
|
|
|
case IRAdd:
|
|
|
|
case IRSub:
|
|
|
|
case IRMul:
|
|
|
|
case IRDiv: {
|
|
|
|
if (lhs->type.kind == TypeInt && rhs->type.kind == TypeInt) {
|
|
|
|
ssize_t res;
|
|
|
|
switch (instr) {
|
|
|
|
case IRAdd: res = lhs->Int + rhs->Int; break;
|
|
|
|
case IRSub: res = lhs->Int - rhs->Int; break;
|
|
|
|
case IRMul: res = lhs->Int * rhs->Int; break;
|
|
|
|
case IRDiv: res = lhs->Int / rhs->Int; break;
|
2021-12-21 13:59:08 +01:00
|
|
|
default: ASSERT_UNREACHED();
|
2021-12-21 01:18:22 +01:00
|
|
|
}
|
|
|
|
return (Value){
|
|
|
|
.type.kind = TypeInt,
|
|
|
|
.Int = res,
|
|
|
|
};
|
|
|
|
} else if (lhs->type.kind == TypeFloat && rhs->type.kind == TypeFloat) {
|
2021-12-26 11:59:01 +01:00
|
|
|
double res;
|
2021-12-21 01:18:22 +01:00
|
|
|
switch (instr) {
|
|
|
|
case IRAdd: res = lhs->Float + rhs->Float; break;
|
|
|
|
case IRSub: res = lhs->Float - rhs->Float; break;
|
|
|
|
case IRMul: res = lhs->Float * rhs->Float; break;
|
|
|
|
case IRDiv: res = lhs->Float / rhs->Float; break;
|
2021-12-21 13:59:08 +01:00
|
|
|
default: ASSERT_UNREACHED();
|
2021-12-21 01:18:22 +01:00
|
|
|
}
|
|
|
|
return (Value){
|
|
|
|
.type.kind = TypeFloat,
|
|
|
|
.Float = res,
|
|
|
|
};
|
|
|
|
} else {
|
2021-12-25 12:32:52 +01:00
|
|
|
set_err("Unsupported types for operation '%s': %s and %s", irinstr_str[instr], type_str[lhs->type.kind], type_str[rhs->type.kind]);
|
2021-12-21 01:18:22 +01:00
|
|
|
return (Value){0};
|
|
|
|
}
|
|
|
|
}
|
2021-12-23 21:06:49 +01:00
|
|
|
case IREq:
|
2021-12-26 12:19:54 +01:00
|
|
|
case IRNeq:
|
2021-12-23 21:06:49 +01:00
|
|
|
case IRLt:
|
2021-12-25 14:06:20 +01:00
|
|
|
case IRLe: {
|
2021-12-23 21:06:49 +01:00
|
|
|
bool res;
|
|
|
|
if (lhs->type.kind == TypeInt && rhs->type.kind == TypeInt) {
|
|
|
|
switch (instr) {
|
2021-12-26 12:19:54 +01:00
|
|
|
case IREq: res = lhs->Int == rhs->Int; break;
|
|
|
|
case IRNeq: res = lhs->Int != rhs->Int; break;
|
|
|
|
case IRLt: res = lhs->Int < rhs->Int; break;
|
|
|
|
case IRLe: res = lhs->Int <= rhs->Int; break;
|
2021-12-23 21:06:49 +01:00
|
|
|
default: ASSERT_UNREACHED();
|
|
|
|
};
|
|
|
|
} else if (lhs->type.kind == TypeFloat && rhs->type.kind == TypeFloat) {
|
|
|
|
switch (instr) {
|
2021-12-26 12:19:54 +01:00
|
|
|
case IREq: res = lhs->Float == rhs->Float; break;
|
|
|
|
case IRNeq: res = lhs->Float != rhs->Float; break;
|
|
|
|
case IRLt: res = lhs->Float < rhs->Float; break;
|
|
|
|
case IRLe: res = lhs->Float <= rhs->Float; break;
|
2021-12-23 21:06:49 +01:00
|
|
|
default: ASSERT_UNREACHED();
|
|
|
|
};
|
2021-12-26 11:36:52 +01:00
|
|
|
} else if (lhs->type.kind == TypeArr && lhs->Arr.type.kind == TypeChar && lhs->Arr.is_string &&
|
|
|
|
rhs->type.kind == TypeArr && rhs->Arr.type.kind == TypeChar && rhs->Arr.is_string) {
|
|
|
|
switch (instr) {
|
|
|
|
case IREq:
|
|
|
|
res = lhs->Arr.len == rhs->Arr.len ? strncmp(lhs->Arr.vals, rhs->Arr.vals, lhs->Arr.len) == 0 : false;
|
|
|
|
break;
|
2021-12-26 12:19:54 +01:00
|
|
|
case IRNeq:
|
|
|
|
res = lhs->Arr.len == rhs->Arr.len ? strncmp(lhs->Arr.vals, rhs->Arr.vals, lhs->Arr.len) != 0 : true;
|
|
|
|
break;
|
2021-12-26 11:36:52 +01:00
|
|
|
default:
|
|
|
|
set_err("String operation '%s' not supported", irinstr_str[instr]);
|
|
|
|
break;
|
|
|
|
};
|
2021-12-23 21:06:49 +01:00
|
|
|
} else {
|
2021-12-25 12:32:52 +01:00
|
|
|
set_err("Unsupported types for operation '%s': %s and %s", irinstr_str[instr], type_str[lhs->type.kind], type_str[rhs->type.kind]);
|
2021-12-23 21:06:49 +01:00
|
|
|
return (Value){0};
|
|
|
|
}
|
|
|
|
return (Value){
|
|
|
|
.type.kind = TypeBool,
|
|
|
|
.Bool = res,
|
|
|
|
};
|
2021-12-25 14:06:20 +01:00
|
|
|
}
|
2021-12-23 21:42:09 +01:00
|
|
|
case IRAnd:
|
|
|
|
return (Value){
|
|
|
|
.type.kind = TypeBool,
|
|
|
|
.Bool = is_nonzero(lhs) && is_nonzero(rhs),
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
case IROr:
|
|
|
|
return (Value){
|
|
|
|
.type.kind = TypeBool,
|
|
|
|
.Bool = is_nonzero(lhs) || is_nonzero(rhs),
|
|
|
|
};
|
|
|
|
break;
|
2021-12-21 01:18:22 +01:00
|
|
|
default:
|
|
|
|
ASSERT_UNREACHED();
|
|
|
|
}
|
|
|
|
return (Value){0};
|
|
|
|
}
|
|
|
|
|
2021-12-22 17:23:24 +01:00
|
|
|
Value eval_unary(IRInstr instr, const Value *v) {
|
|
|
|
switch (instr) {
|
|
|
|
case IRSet:
|
|
|
|
return *v;
|
|
|
|
case IRNeg:
|
|
|
|
if (v->type.kind == TypeInt)
|
|
|
|
return (Value){ .type.kind = TypeInt, .Int = -v->Int };
|
|
|
|
else if (v->type.kind == TypeFloat)
|
|
|
|
return (Value){ .type.kind = TypeFloat, .Float = -v->Float };
|
|
|
|
else {
|
2021-12-25 12:32:52 +01:00
|
|
|
set_err("Unsupported type for operation '%s': %s", irinstr_str[instr], type_str[v->type.kind]);
|
2021-12-23 21:06:49 +01:00
|
|
|
return (Value){0};
|
|
|
|
}
|
|
|
|
case IRNot:
|
|
|
|
if (v->type.kind == TypeBool) {
|
|
|
|
return (Value){ .type.kind = TypeBool, .Bool = !v->Bool };
|
|
|
|
} else {
|
2021-12-25 12:32:52 +01:00
|
|
|
set_err("Unsupported type for operation '%s': %s", irinstr_str[instr], type_str[v->type.kind]);
|
2021-12-22 17:23:24 +01:00
|
|
|
return (Value){0};
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
ASSERT_UNREACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_nonzero(const Value *v) {
|
|
|
|
switch (v->type.kind) {
|
|
|
|
case TypeInt: return v->Int != 0;
|
|
|
|
case TypeFloat: return v->Float != 0.0;
|
2021-12-23 21:06:49 +01:00
|
|
|
case TypeBool: return v->Bool;
|
2021-12-22 17:23:24 +01:00
|
|
|
default: ASSERT_UNREACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-21 01:18:22 +01:00
|
|
|
Value zero_val(Type ty) {
|
|
|
|
Value ret;
|
|
|
|
ret.type = ty;
|
|
|
|
switch (ty.kind) {
|
2021-12-23 21:06:49 +01:00
|
|
|
case TypeInt: ret.Int = 0; break;
|
|
|
|
case TypeFloat: ret.Float = 0.0; break;
|
|
|
|
case TypeBool: ret.Bool = false; break;
|
2021-12-21 01:18:22 +01:00
|
|
|
default: ASSERT_UNREACHED();
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|