3 m4_include(«mozart
.hh»
)
5 class NewBlockAllocator
: public BlockAllocator
{
6 MemoryBlock
* request(size_t size
, size_t alignment
){
7 if(!--memAllocs
){std::cerr
<<"out of memory"<<std::endl
; exit(1);}
8 MemoryBlock
* m
=(MemoryBlock
*) operator new(size
+alignment
+sizeof(MemoryBlock
));
9 m
->pos
=((byte
*)m
)+sizeof(MemoryBlock
);
10 m
->pastEnd
=((byte
*)m
)+size
+alignment
+sizeof(MemoryBlock
);
13 void release(MemoryBlock
* block
){
14 operator delete(block
);
17 bool sched(){static size_t f
=0; return ((++f
)%193)==0;}
18 bool inter(){static size_t f
=0; return ((++f
)%193)==0;}
19 void mkMainCode(VM
&,Node
&,size_t,size_t);
20 void mkFibonacci(VM
&,Node
&,size_t,size_t);
21 void mkNaiveFibonacci(VM
&,Node
&,size_t,size_t);
22 VM vm
=VM(blocks
, sched
, inter
);
23 int main(int argc
, char* argv
[]){
24 UnstableNode
& mainThread
=*new(vm
)UnstableNode();
25 mkThread(vm
, mainThread
);
26 StableNode
& mainCode
=*new(vm
)StableNode();
27 mkMainCode(vm
,mainCode
,0,3);
28 StableNode
& mainProc
=*new(vm
)StableNode();
29 StableNode
* gMainProc
=mkAbstraction(vm
,mainProc
,mainCode
,0,3);
30 mkSmallint(vm
,*gMainProc
++,37);
32 StableNode
& fibCode
=*new(vm
)StableNode();
33 mkFibonacci(vm
,fibCode
,5,0);
34 mkAbstraction(vm
,*gMainProc
++,fibCode
,5,0);
36 StableNode
& naiveFibCode
=*new(vm
)StableNode();
37 mkNaiveFibonacci(vm
,naiveFibCode
,2,1);
38 StableNode
& naiveFibProc
=*new(vm
)StableNode();
39 StableNode
* gNaiveFibProc
=mkAbstraction(vm
,naiveFibProc
,naiveFibCode
,2,1);
40 set(*gMainProc
++,naiveFibProc
);
41 set(*gNaiveFibProc
++,naiveFibProc
);
43 vm
.pushCall(mainThread
,mainProc
);
44 vm
.schedule(mainThread
);
45 while(vm
.run()){vm
.gc();}
49 BIStatus
* intLess(VM
&,UnstableNode
* args
[]){
50 Node
&n0
=deref(*args
[0]);
51 Node
&n1
=deref(*args
[1]);
52 if(n0
.vt
!=&smallintVT
)std::cout
<<"n0 is not an integer"<<std::endl
;
53 if(n1
.vt
!=&smallintVT
)std::cout
<<"n1 is not an integer"<<std::endl
;
54 mkBoolean(vm
,*args
[2],n0
.c
.si
<n1
.c
.si
);
57 BIStatus
* intSum(VM
&,UnstableNode
* args
[]){
58 Node
&n0
=deref(*args
[0]);
59 Node
&n1
=deref(*args
[1]);
60 if(n0
.vt
!=&smallintVT
)std::cout
<<"n0 is not an integer"<<std::endl
;
61 if(n1
.vt
!=&smallintVT
)std::cout
<<"n1 is not an integer"<<std::endl
;
62 mkSmallint(vm
,*args
[2],n0
.c
.si
+n1
.c
.si
);
65 BIStatus
* printInt(VM
&,UnstableNode
* args
[]){
66 Node
&n0
=deref(*args
[0]);
67 if(n0
.vt
!=&smallintVT
)std::cout
<<"n0 is not an integer"<<std::endl
;
68 std::cout
<<n0
.c
.si
<<std::endl
;
71 BIStatus
* unify(VM
&,UnstableNode
* args
[]){
72 Node
&n0
=deref(*args
[0]);
73 Node
&n1
=deref(*args
[1]);
74 if(n0
.vt
!=&simpleVarVT
)std::cout
<<"n0 is not an unbound variable"<<std::endl
;
76 set(n0
,(StableNode
&)n1
);
79 set((UnstableNode
&)n0
,(UnstableNode
&)n1
,vm
);
81 set((StableNode
&)n0
,(UnstableNode
&)n1
);
84 BIStatus
* makeVar(VM
&vm
,UnstableNode
* args
[]){
85 mkSimpleVar(vm
,*args
[0]);
90 m4_include(«threads
.cc»
)
91 m4_include(«vtable
.cc»
)
92 m4_include(«boolean
.cc»
)
93 m4_include(«codeBlock
.cc»
)
94 m4_include(«abstraction
.cc»
)
95 m4_include(«integer
.cc»
)
96 m4_include(«builtin
.cc»
)
97 m4_include(«simpleVar
.cc»
)
101 m4_include(«codeBlock
.m4»
)
105 allocateY(«i
.count
=1;»
)
106 callBI1(«
mkBuiltin(vm
,i
.bi
,&printInt
); i
.params
[0]=&vm
.X
[1]; i
.liveXs
=31;»
)
107 copyYX(«i
.dest
=0; i
.src
=&vm
.X
[1];»
)
108 callBI3(«
mkBuiltin(vm
,i
.bi
,&intLess
); i
.params
[0]=&vm
.X
[0]; i
.params
[1]=&vm
.X
[3]; i
.params
[2]=&vm
.X
[5]; i
.liveXs
=31;»
)
109 ifX(«i
.cond
=&vm
.X
[5]; else1
=&i
.ifFalse
; i
.liveXs
=63;»
)
110 callBI3(«
mkBuiltin(vm
,i
.bi
,&intSum
); i
.params
[0]=&vm
.X
[1]; i
.params
[1]=&vm
.X
[2]; i
.params
[2]=&vm
.X
[5]; i
.liveXs
=31;»
)
111 copyXX(«i
.dest
=&vm
.X
[1]; i
.src
=&vm
.X
[2];»
)
112 copyXX(«i
.dest
=&vm
.X
[2]; i
.src
=&vm
.X
[5];»
)
113 copyXC(«i
.dest
=&vm
.X
[5]; mkSmallint(vm
,i
.src
,1);»
)
114 callBI3(«
mkBuiltin(vm
,i
.bi
,&intSum
); i
.params
[0]=&vm
.X
[0]; i
.params
[1]=&vm
.X
[5]; i
.params
[2]=&vm
.X
[0]; i
.liveXs
=63;»
)
115 callX(«i
.callee
=&vm
.X
[4]; i
.arity
=5;»
)
116 copyXY(«i
.dest
=&vm
.X
[1]; i
.src
=0;»
)
117 callBI1(«
mkBuiltin(vm
,i
.bi
,&printInt
); i
.params
[0]=&vm
.X
[1]; i
.liveXs
=2;»
)
118 deallocateY(«i
.liveXs
=0;»
)
120 callBI1(«
*else1
=w
; mkBuiltin(vm
,i
.bi
,&printInt
); i
.params
[0]=&vm
.X
[3]; i
.liveXs
=8;»
)
121 copyXY(«i
.dest
=&vm
.X
[1]; i
.src
=0;»
)
122 callBI1(«
mkBuiltin(vm
,i
.bi
,&printInt
); i
.params
[0]=&vm
.X
[1]; i
.liveXs
=2;»
)
123 deallocateY(«i
.liveXs
=0;»
)
125 PP_CXX_VMA_END(«mkFibonacci»
)
129 copyXC(«i
.dest
=&vm
.X
[2]; mkSmallint(vm
,i
.src
,2);»
)
130 callBI3(«
mkBuiltin(vm
,i
.bi
,&intLess
); i
.params
[0]=&vm
.X
[0]; i
.params
[1]=&vm
.X
[2]; i
.params
[2]=&vm
.X
[2]; i
.liveXs
=7;»
)
131 ifX(«i
.cond
=&vm
.X
[2]; else2
=&i
.ifFalse
; i
.liveXs
=7;»
)
132 copyXC(«i
.dest
=&vm
.X
[2]; mkSmallint(vm
,i
.src
,1);»
)
133 callBI2(«
mkBuiltin(vm
,i
.bi
,&unify
); i
.params
[0]=&vm
.X
[1]; i
.params
[1]=&vm
.X
[2]; i
.liveXs
=6;»
)
135 allocateY(«
*else2
=w
; i
.count
=3;»
)
136 copyXC(«i
.dest
=&vm
.X
[2]; mkSmallint(vm
,i
.src
,-1);»
)
137 callBI3(«
mkBuiltin(vm
,i
.bi
,&intSum
); i
.params
[0]=&vm
.X
[0]; i
.params
[1]=&vm
.X
[2]; i
.params
[2]=&vm
.X
[0]; i
.liveXs
=7;»
)
138 callBI3(«
mkBuiltin(vm
,i
.bi
,&intSum
); i
.params
[0]=&vm
.X
[0]; i
.params
[1]=&vm
.X
[2]; i
.params
[2]=&vm
.X
[3]; i
.liveXs
=7;»
)
139 movYX(«i
.dest
=0; i
.src
=&vm
.X
[3];»
)
140 movYX(«i
.dest
=1; i
.src
=&vm
.X
[1];»
)
141 m4_dnl
callBI1(«
mkBuiltin(vm
,i
.bi
,&makeVar
); i
.params
[0]=&vm
.X
[1]; i
.liveXs
=3;»
)
142 m4_dnl
copyYX(«i
.dest
=2; i
.src
=&vm
.X
[1];»
)
143 createVarXY(«i
.dest1
=&vm
.X
[1]; i
.dest2
=2;»
)
144 copyXG(«i
.dest
=&vm
.X
[3]; i
.src
=0;»
)
145 callX(«i
.callee
=&vm
.X
[3];» i
.arity
=2;)
146 movXY(«i
.dest
=&vm
.X
[0]; i
.src
=0;»
)
147 m4_dnl
callBI1(«
mkBuiltin(vm
,i
.bi
,&makeVar
); i
.params
[0]=&vm
.X
[1]; i
.liveXs
=0;»
)
148 m4_dnl
copyYX(«i
.dest
=0; i
.src
=&vm
.X
[1];»
)
149 createVarXY(«i
.dest1
=&vm
.X
[1]; i
.dest2
=0;»
)
150 copyXG(«i
.dest
=&vm
.X
[3]; i
.src
=0;»
)
151 callX(«i
.callee
=&vm
.X
[3]; i
.arity
=2;»
)
152 movXY(«i
.dest
=&vm
.X
[0]; i
.src
=1;»
)
153 movXY(«i
.dest
=&vm
.X
[1]; i
.src
=2;»
)
154 movXY(«i
.dest
=&vm
.X
[2]; i
.src
=0;»
)
155 deallocateY(«i
.liveXs
=7;»
)
156 callBI3(«
mkBuiltin(vm
,i
.bi
,&intSum
); i
.params
[0]=&vm
.X
[1]; i
.params
[1]=&vm
.X
[2]; i
.params
[2]=&vm
.X
[3]; i
.liveXs
=7;»
)
157 callBI2(«
mkBuiltin(vm
,i
.bi
,&unify
); i
.params
[0]=&vm
.X
[0]; i
.params
[1]=&vm
.X
[3]; i
.liveXs
=9;»
)
159 PP_CXX_VMA_END(«mkNaiveFibonacci»
)
161 m4_define(«PP_FIB_VERSION»
,«naive»
)
163 //m4_ifelse(PP_FIB_VERSION,«linear»,«
164 copyXC(«i
.dest
=&vm
.X
[0]; mkSmallint(vm
,i
.src
,1);»
)
165 copyXX(«i
.dest
=&vm
.X
[1]; i
.src
=&vm
.X
[0];»
)
166 copyXX(«i
.dest
=&vm
.X
[2]; i
.src
=&vm
.X
[0];»
)
167 copyXG(«i
.dest
=&vm
.X
[3]; i
.src
=0;»
)
168 copyXG(«i
.dest
=&vm
.X
[4]; i
.src
=1;»
)
169 tailCallX(«i
.callee
=&vm
.X
[4];» i
.arity
=5;)
172 //m4_ifelse(PP_FIB_VERSION,«naive»,«
173 allocateY(«i
.count
=1;»
)
174 copyXG(«i
.dest
=&vm
.X
[0]; i
.src
=0;»
)
175 callBI1(«
mkBuiltin(vm
,i
.bi
,&makeVar
); i
.params
[0]=&vm
.X
[1]; i
.liveXs
=1;»
)
176 copyYX(«i
.dest
=0; i
.src
=&vm
.X
[1];»
)
177 copyXG(«i
.dest
=&vm
.X
[2]; i
.src
=2;»
)
178 callBI1(«
mkBuiltin(vm
,i
.bi
,&printInt
); i
.params
[0]=&vm
.X
[0]; i
.liveXs
=7;»
)
179 callX(«i
.callee
=&vm
.X
[2]; i
.arity
=2;»
)
180 copyXY(«i
.dest
=&vm
.X
[0]; i
.src
=0;»
)
181 deallocateY(«i
.liveXs
=1;»
)
182 callBI1(«
mkBuiltin(vm
,i
.bi
,&printInt
); i
.params
[0]=&vm
.X
[0]; i
.liveXs
=1;»
)
185 PP_CXX_VMA_END(«mkMainCode»
)