-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest3.c
63 lines (53 loc) · 1.19 KB
/
test3.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* SVM Test 3
* by Takuo Watanabe
*/
#include <stdio.h>
#include <stdlib.h>
#include "vm.h"
#include "fun.h"
#define VS_SIZE 256
#define GV_SIZE 8
static Value vs[VS_SIZE];
static Value gv[GV_SIZE];
static Value *vs_base = vs + VS_SIZE;
static Bcode code[] = {
/*
show f(n, 1);
halt;
*/
/* 0 */ i_giload, 0, 0,
/* 3 */ i_bipush, 1,
/* 5 */ i_gaload, 0, 1,
/* 8 */ i_icall, 2, /* f(n, 1) */
/* 10 */ i_showtop,
/* 11 */ i_halt,
/*
int f (int n, int a) {
if (n <= 0)
return a;
else
return f(n - 1, n * a);
}
*/
/* 12 */ i_iload, 0,
/* 14 */ i_ifgt, 0, 3,
/* 17 */ i_iload, 1,
/* 19 */ i_iret,
/* 20 */ i_iload, 0,
/* 22 */ i_bipush, 1,
/* 24 */ i_isub,
/* 25 */ i_iload, 0,
/* 27 */ i_iload, 1,
/* 29 */ i_imul,
/* 30 */ i_gaload, 0, 1,
/* 33 */ i_sicall, 2,
};
void vmtest (int n, vm_mode_t trace) {
int codelen = sizeof(code)/sizeof(Bcode);
Function f = new_function(2, 0, code + 12, codelen - 12);
gv[0] = (Value)n;
gv[1] = (Value)f;
vm(code, codelen, vs_base, VS_SIZE, gv, GV_SIZE, trace);
delete_function(f);
}