summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-09-29 16:41:37 +0100
committerDamien George <damien.p.george@gmail.com>2014-09-29 19:42:06 +0100
commit6f81348fa25216f03686b342765f337ab57e2e5f (patch)
treef0fc55b32521dde22764875deffd9cd92db60636
parenta7329615eb5b55869ae8a460f9b9be5be04a5bee (diff)
py: Allow viper to use ints as direct conditionals in jumps.
Allows things like: if 1: ...
-rw-r--r--py/emitnative.c33
-rw-r--r--tests/micropython/viper_cond.py15
-rw-r--r--tests/micropython/viper_cond.py.exp2
3 files changed, 36 insertions, 14 deletions
diff --git a/py/emitnative.c b/py/emitnative.c
index c2aa7a76b..cfcba27a1 100644
--- a/py/emitnative.c
+++ b/py/emitnative.c
@@ -1365,20 +1365,25 @@ STATIC void emit_native_jump(emit_t *emit, mp_uint_t label) {
STATIC void emit_native_jump_helper(emit_t *emit, mp_uint_t label, bool pop) {
vtype_kind_t vtype = peek_vtype(emit);
- if (vtype == VTYPE_BOOL) {
- emit_pre_pop_reg(emit, &vtype, REG_RET);
- if (!pop) {
- adjust_stack(emit, 1);
- }
- } else if (vtype == VTYPE_PYOBJ) {
- emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
- if (!pop) {
- adjust_stack(emit, 1);
- }
- emit_call(emit, MP_F_OBJ_IS_TRUE);
- } else {
- printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
- assert(0);
+ switch (vtype) {
+ case VTYPE_PYOBJ:
+ emit_pre_pop_reg(emit, &vtype, REG_ARG_1);
+ if (!pop) {
+ adjust_stack(emit, 1);
+ }
+ emit_call(emit, MP_F_OBJ_IS_TRUE);
+ break;
+ case VTYPE_BOOL:
+ case VTYPE_INT:
+ case VTYPE_UINT:
+ emit_pre_pop_reg(emit, &vtype, REG_RET);
+ if (!pop) {
+ adjust_stack(emit, 1);
+ }
+ break;
+ default:
+ printf("ViperTypeError: expecting a bool or pyobj, got %d\n", vtype);
+ assert(0);
}
// need to commit stack because we may jump elsewhere
need_stack_settled(emit);
diff --git a/tests/micropython/viper_cond.py b/tests/micropython/viper_cond.py
new file mode 100644
index 000000000..258a37812
--- /dev/null
+++ b/tests/micropython/viper_cond.py
@@ -0,0 +1,15 @@
+# using a bool as a conditional
+@micropython.viper
+def f():
+ x = True
+ if x:
+ print("x", x)
+f()
+
+# using an int as a conditional
+@micropython.viper
+def g():
+ y = 1
+ if y:
+ print("y", y)
+g()
diff --git a/tests/micropython/viper_cond.py.exp b/tests/micropython/viper_cond.py.exp
new file mode 100644
index 000000000..244817f1f
--- /dev/null
+++ b/tests/micropython/viper_cond.py.exp
@@ -0,0 +1,2 @@
+x True
+y 1