Added cons.
[mozart2.git] / vm.hh
blobe9bfa9d9f79c29042d82c2e43291469f38a3f586
1 const size_t X_COUNT=1<<8;
2 const size_t INSTR_COUNT=PP_INSTR_COUNT;
3 enum StackFrames{
4 S_CONT,
5 S_GC_CONT,
6 S_X,
7 STACK_COUNT
8 };
9 struct StackFrameHeader{
10 StackFrameHeader* next;
11 Instr kind;
13 struct ContStackFrame:public StackFrameHeader{
14 Instr* startIP;
15 Instr* IP;
16 size_t numY;
17 UnstableNode* Y;
18 StableNode* G;
20 struct GCContStackFrame:public StackFrameHeader{
21 size_t offset;
22 size_t numY;
23 UnstableNode* Y;
24 UnstableNode abstr;
26 //struct LocalStackFrame:public StackFrameHeader{};
27 struct SavedX{
28 size_t x;
29 UnstableNode r;
31 struct XStackFrame:public StackFrameHeader{
32 size_t count;
33 SavedX* ptr;
35 class VM{
36 public:
37 VM(BlockAllocator &ba,
38 bool (*_scheduleNext)(),
39 bool (*_interrupted)() ):
40 instrTable(),
41 stackTable(),
42 X(),
43 uniques(*this),
44 active(ba),
45 shadow(ba),
46 bigEmulAlloc(),
47 smallEmulAlloc(),
48 bigEmulAllocShadow(),
49 smallEmulAllocShadow(),
50 threads(),
51 initialized(0),
52 scheduleNext(_scheduleNext),
53 interrupted(_interrupted)
54 {run();}
55 bool run();
56 void pushCall(Node &thread, Node& proc);
57 void schedule(UnstableNode &thread){
58 threads.schedule(*this, thread);
60 void gc();
61 void gcStack(GC &gc, StackFrameHeader* &from, StackFrameHeader* &to);
62 Instr instrTable[INSTR_COUNT];
63 Instr stackTable[STACK_COUNT];
64 UnstableNode X[X_COUNT];
65 UniqueController uniques;
66 private:
67 friend void* operator new(size_t s, VM& vm);
68 MemPool active;
69 MemPool shadow;
70 FastAllocator<4,6> bigEmulAlloc;
71 FastAllocator<0,6> smallEmulAlloc;
72 FastAllocator<4,6> bigEmulAllocShadow;
73 FastAllocator<0,6> smallEmulAllocShadow;
74 template<class T> T* alloc(size_t count){
75 size_t s=((count*sizeof(T))+(sizeof(void*)-1))/sizeof(void*);
76 if(s<(1<<6)){
77 return (T*)smallEmulAlloc.alloc(active,s);
78 }else{
79 return (T*)bigEmulAlloc.alloc(active,s);
82 template<class T> void free(T* ptr, size_t count){
83 size_t s=((count*sizeof(T))+(sizeof(void*)-1))/sizeof(void*);
84 if(s<(1<<6)){
85 smallEmulAlloc.free((char*)ptr,s);
86 }else{
87 bigEmulAlloc.free((char*)ptr,s);
90 template<class T> T* allocShadow(size_t count){
91 size_t s=((count*sizeof(T))+(sizeof(void*)-1))/sizeof(void*);
92 if(s<(1<<6)){
93 return (T*)smallEmulAllocShadow.alloc(shadow,s);
94 }else{
95 return (T*)bigEmulAllocShadow.alloc(shadow,s);
98 template<class T> void freeShadow(T* ptr, size_t count){
99 size_t s=((count*sizeof(T))+(sizeof(void*)-1))/sizeof(void*);
100 if(s<(1<<6)){
101 smallEmulAllocShadow.free((char*)ptr,s);
102 }else{
103 bigEmulAllocShadow.free((char*)ptr,s);
106 friend class ThreadPool;
107 ThreadPool threads;
108 bool initialized;
109 bool (*scheduleNext)();
110 bool (*interrupted)();
112 void* operator new(size_t s, VM& vm){
113 return vm.active.alloc(s,DEFAULT_ALIGNMENT);
115 //PP_FOR_EACH_INSTR(«
116 struct s_«»PP_INSTR_NAME {
117 Instr instr;
118 PP_FOR_EACH_INSTR_PARAM(«
119 m4_ifelse(m4_defn(«PP_INSTR_PARAM_TYPE»),«WX»,«UnstableNode* »PP_INSTR_PARAM_NAME«;»,
120 m4_defn(«PP_INSTR_PARAM_TYPE»),«RX»,«UnstableNode* »PP_INSTR_PARAM_NAME«;»,
121 m4_defn(«PP_INSTR_PARAM_TYPE»),«WY»,«size_t »PP_INSTR_PARAM_NAME«;»,
122 m4_defn(«PP_INSTR_PARAM_TYPE»),«RY»,«size_t »PP_INSTR_PARAM_NAME«;»,
123 m4_defn(«PP_INSTR_PARAM_TYPE»),«RG»,«size_t »PP_INSTR_PARAM_NAME«;»,
124 m4_defn(«PP_INSTR_PARAM_TYPE»),«V»,«StableNode »PP_INSTR_PARAM_NAME«;»,
125 m4_defn(«PP_INSTR_PARAM_TYPE»),«S»,«size_t »PP_INSTR_PARAM_NAME«;»,
126 m4_defn(«PP_INSTR_PARAM_TYPE»),«I»,«Instr* »PP_INSTR_PARAM_NAME«;»,
127 m4_defn(«PP_INSTR_PARAM_TYPE»),«PA»,«size_t »PP_INSTR_PARAM_NAME«;»,
128 m4_defn(«PP_INSTR_PARAM_TYPE»),«LX»,«long »PP_INSTR_PARAM_NAME«;»,
129 m4_regexp(m4_defn(«PP_INSTR_PARAM_TYPE»),«X\[\([0-9]*\)\]»,«UnstableNode* »PP_INSTR_PARAM_NAME«[\1];»))»,«»)
131 //»,«»)
132 enum Instructions{
133 PP_FOR_EACH_INSTR(«I_«»PP_INSTR_NAME»,«,»)