Browse Source

Fixing jmp operations

Yann Weber 6 years ago
parent
commit
545d33cd76
2 changed files with 12 additions and 4 deletions
  1. 8
    4
      libs/turmit.c
  2. 4
    0
      libs/turmit.h

+ 8
- 4
libs/turmit.c View File

@@ -101,7 +101,9 @@ TURMIT_OP(swp)
101 101
 
102 102
 TURMIT_OP(jmp)
103 103
 {
104
-	turmit->op_cur += SPOP(turmit) - 1;
104
+	turmit_int a;
105
+	a = SPOP(turmit);
106
+	TURMIT_JMP(turmit, a);
105 107
 }
106 108
 
107 109
 TURMIT_OP(jz)
@@ -111,7 +113,7 @@ TURMIT_OP(jz)
111 113
 	b = SPOP(turmit);
112 114
 	if(b == 0)
113 115
 	{
114
-		turmit->op_cur += a - 1;
116
+		TURMIT_JMP(turmit, a);
115 117
 	}
116 118
 }
117 119
 
@@ -125,7 +127,7 @@ TURMIT_OP(jcmp)
125 127
 	if(((cnd & 7) == 0 && a != b) || (cnd > 6) || ((cnd & 1) && a == b) ||
126 128
 		((cnd & 7) > 2 && a < b) || ((cnd & 7) > 4 && a > b))
127 129
 	{
128
-		turmit->op_cur += offset - 1;
130
+		TURMIT_JMP(turmit, offset);
129 131
 	}
130 132
 }
131 133
 
@@ -152,6 +154,7 @@ turmit_t *turmit_init(turmit_t *turmit, ssize_t stack_sz, ssize_t int_max,
152 154
 	turmit->op_expr = NULL;
153 155
 	turmit->op_expr_sz = 0;
154 156
 	turmit->op_cur = 0;
157
+	turmit->op_end = 0;
155 158
 	turmit->stack_sz = stack_sz;
156 159
 	turmit->stack_cur = turmit->stack_sz - 1;
157 160
 	turmit->stack = malloc(sizeof(turmit_int) * turmit->stack_sz);
@@ -225,7 +228,8 @@ turmit_int turmit_exec(turmit_t *turmit, const turmit_int args[5])
225 228
 	turmit_op_t *op;
226 229
 
227 230
 	turmit->op_cur = 0;
228
-	while(turmit->op_cur < turmit->op_expr_sz)
231
+	turmit->op_end = 0;
232
+	while(!turmit->op_end && turmit->op_cur < turmit->op_expr_sz)
229 233
 	{
230 234
 		op = &(turmit->op_expr[turmit->op_cur]);
231 235
 		switch(op->value)

+ 4
- 0
libs/turmit.h View File

@@ -22,6 +22,9 @@ typedef void (*turmit_op_f)(turmit_t*);
22 22
 #define SPOP(t) (__SPOP(t))
23 23
 #define SPUSH(t, v) (t->stack[SINC(t)] = v % t->int_max)
24 24
 
25
+#define TURMIT_JMP(t, v) (((t->op_expr_sz - t->op_cur) <= v)?\
26
+	(t->op_end=1):\
27
+	(t->op_cur += (v>0?v-1:0) ))
25 28
 
26 29
 #define TURMIT_EXPR_MAX_SZ	0xFFFFFFFF
27 30
 
@@ -92,6 +95,7 @@ struct turmit_s
92 95
 	turmit_op_t *op_expr;
93 96
 	ssize_t op_expr_sz;
94 97
 	ssize_t op_cur;
98
+	short op_end;
95 99
 
96 100
 	ssize_t stack_sz;
97 101
 	turmit_int *stack;

Loading…
Cancel
Save