Mercurial > repos > rhope
annotate test/fib.c @ 43:709df3e82bb4
Added methods to support type inference
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Thu, 15 Oct 2009 21:32:43 -0400 |
parents | b71bab34ec65 |
children | d4b44ae2e34a |
rev | line source |
---|---|
11
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 #include <stdio.h> |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 #include "builtin.h" |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 #include "object.h" |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 #include "context.h" |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 #include "func.h" |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
6 #include "integer.h" |
11
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 typedef struct |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 { |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 object * work1; |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 object * work2; |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
12 object * out; |
11
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 } fib_t; |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 object * const_1; |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 object * const_2; |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 Func(Fib, |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 NumParams 1, |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 CallSpace 2, |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 fib_t); |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
23 locals->out = NULL; |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
24 |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
25 |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
26 call->params[0] = add_ref(cdata->params[0]); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
27 call->params[1] = add_ref(const_2); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
28 MCall(METHOD_LESS, 2) |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
29 |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
30 MCall(METHOD_IF, 1) |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
31 |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
32 if(call->params[0]) |
15
b71bab34ec65
Added forgotten release_ref to fib test
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
33 { |
b71bab34ec65
Added forgotten release_ref to fib test
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
34 release_ref(call->params[0]); |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
35 locals->out = add_ref(const_1); |
15
b71bab34ec65
Added forgotten release_ref to fib test
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
36 } |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
37 |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
38 if(call->params[1]) |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
39 { |
15
b71bab34ec65
Added forgotten release_ref to fib test
Mike Pavone <pavone@retrodev.com>
parents:
12
diff
changeset
|
40 release_ref(call->params[1]); |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
41 call->params[0] = add_ref(cdata->params[0]); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
42 call->params[1] = add_ref(const_1); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
43 MCall(METHOD_SUB, 2) |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
44 locals->work1 = call->params[0]; |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
45 |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
46 call->params[0] = add_ref(cdata->params[0]); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
47 call->params[1] = add_ref(const_2); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
48 MCall(METHOD_SUB, 2) |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
49 locals->work2 = call->params[0]; |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
50 |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
51 call->params[0] = locals->work1; |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
52 Call(Fib, 1) |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
53 locals->work1 = call->params[0]; |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
54 |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
55 call->params[0] = locals->work2; |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
56 Call(Fib, 1) |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
57 locals->work2 = call->params[0]; |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
58 |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
59 if(!locals->out) |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
60 { |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
61 release_ref(cdata->params[0]); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
62 cdata->params[0] = locals->work1; |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
63 cdata->params[1] = locals->work2; |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
64 TMCall(METHOD_ADD, 2) |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
65 } |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
66 } |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
67 release_ref(cdata->params[0]); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
68 Ret(0, locals->out) |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
69 |
11
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 EndFunc |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 FuncNoLocals(Main, |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
73 NumParams 1, |
11
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 CallSpace 2) |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
76 TCall(Fib, 1) |
11
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
77 EndFunc |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
79 #ifdef _WIN32 |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
80 #include "windows.h" |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
81 #endif |
11
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 int main(int argc, char ** argv) |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 { |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 returntype ret; |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 calldata *cdata; |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
87 context * ct; |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
88 #ifdef _WIN32 |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
89 DWORD dur; |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
90 dur = timeGetTime(); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
91 #endif |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
92 if(argc < 2) |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
93 { |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
94 puts("not enought arguments"); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
95 return -1; |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
96 } |
11
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
97 register_builtin_types(); |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
98 ct = new_context(); |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
99 cdata = alloc_cdata(ct, 1); |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
100 cdata->num_params = 1; |
11
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
101 cdata->resume = 0; |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
102 |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
103 const_1 = make_Int32(1); |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
104 const_2 = make_Int32(2); |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
105 cdata->params[0] = make_Int32(atoi(argv[1])); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
106 ret = f_Main(cdata); |
11
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
107 while(ret == TAIL_RETURN) |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
108 ret = cdata->tail_func(cdata); |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
109 if(ret == EXCEPTION_RETURN) { |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
110 puts("Exception!"); |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
111 return -1; |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
112 } |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
113 printf("Result: %d\n", ((t_Int32 *)cdata->params[0])->num); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
114 #ifdef _WIN32 |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
115 dur = timeGetTime()-dur; |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
116 printf("Took %d milliseconds\n", dur); |
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
11
diff
changeset
|
117 #endif |
11
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
118 return 0; |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
119 } |
3021dac0d8f5
Stack unwind is so close I can almost taste it
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
120 |