summaryrefslogtreecommitdiffstats
path: root/compile.c
diff options
context:
space:
mode:
authorNicolas Williams <nico@cryptonector.com>2014-07-27 17:33:22 -0500
committerNicolas Williams <nico@cryptonector.com>2014-07-27 17:48:49 -0500
commit2e2538ccb85a39bfe6364d2e7a863e04d6ece9c5 (patch)
tree6c86cfbd14e8b28b374c5e2fbfc3f8d1c36ae1dc /compile.c
parentae27178352e1bd78bbfed31bc8593e8b01ebd397 (diff)
Fold constants (fix #504)
Diffstat (limited to 'compile.c')
-rw-r--r--compile.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/compile.c b/compile.c
index 0b0208ee..03d03c3e 100644
--- a/compile.c
+++ b/compile.c
@@ -97,7 +97,7 @@ static block inst_block(inst* i) {
return b;
}
-static int block_is_single(block b) {
+int block_is_single(block b) {
return b.first && b.first == b.last;
}
@@ -131,6 +131,10 @@ block gen_noop() {
return b;
}
+int block_is_noop(block b) {
+ return (b.first == 0 && b.last == 0);
+}
+
block gen_op_simple(opcode op) {
assert(opcode_describe(op)->length == 1);
return inst_block(inst_new(op));
@@ -144,6 +148,20 @@ block gen_const(jv constant) {
return inst_block(i);
}
+int block_is_const(block b) {
+ return (block_is_single(b) && b.first->op == LOADK);
+}
+
+jv_kind block_const_kind(block b) {
+ assert(block_is_const(b));
+ return jv_get_kind(b.first->imm.constant);
+}
+
+jv block_const(block b) {
+ assert(block_is_const(b));
+ return jv_copy(b.first->imm.constant);
+}
+
block gen_op_target(opcode op, block target) {
assert(opcode_describe(op)->flags & OP_HAS_BRANCH);
assert(target.last);