File tree Expand file tree Collapse file tree 1 file changed +12
-5
lines changed Expand file tree Collapse file tree 1 file changed +12
-5
lines changed Original file line number Diff line number Diff line change 1
1
//
2
2
// Created by xj on 2021/3/30.
3
3
//
4
+ #include <math.h>
5
+ #include <stdint.h>
4
6
#include <stdio.h>
5
7
#include <stdlib.h>
6
- #include <stdint.h>
7
- #include <math.h>
8
8
#include <string.h>
9
9
#include "deep_interp.h"
10
10
#include "deep_loader.h"
11
+ #include "deep_log.h"
11
12
#include "deep_mem.h"
12
13
#include "deep_opcode.h"
13
14
#include "deep_log.h"
26
27
27
28
#define STACK_CAPACITY 100
28
29
30
+ // 安全除法
31
+ #define DIVIDE (TYPE , DIVIDEND , DIVISOR ) \
32
+ (((TYPE)DIVISOR == 0) && \
33
+ (error("Arithmetic Error: Divide by Zero!"), exit(1), 0) \
34
+ || (TYPE)DIVIDEND / (TYPE)DIVISOR)
35
+
29
36
//创建操作数栈
30
37
DEEPStack * stack_cons (void ) {
31
38
DEEPStack * stack = (DEEPStack * ) deep_malloc (sizeof (DEEPStack ));
@@ -158,14 +165,14 @@ void exec_instructions(DEEPExecEnv *current_env, DEEPModule *module) {
158
165
ip ++ ;
159
166
int32_t a = popS32 ();
160
167
int32_t b = popS32 ();
161
- pushS32 (b / a );
168
+ pushS32 (DIVIDE ( int32_t , b , a ) );
162
169
break ;
163
170
}
164
171
case i32_divu : {
165
172
ip ++ ;
166
173
uint32_t a = popU32 ();
167
174
uint32_t b = popU32 ();
168
- pushU32 (b / a );
175
+ pushU32 (DIVIDE ( uint32_t , b , a ) );
169
176
break ;
170
177
}
171
178
case i32_const : {
@@ -242,7 +249,7 @@ void exec_instructions(DEEPExecEnv *current_env, DEEPModule *module) {
242
249
ip ++ ;
243
250
float a = popF32 ();
244
251
float b = popF32 ();
245
- pushF32 (b / a );
252
+ pushF32 (DIVIDE ( float , b , a ) );
246
253
break ;
247
254
}
248
255
case f32_min : {
You can’t perform that action at this time.
0 commit comments