more runtime stuff
[IRC.git] / Robust / src / Runtime / runtime.c
1 #include "runtime.h"
2 #include "structdefs.h"
3 #include <string.h>
4
5 extern int classsize[];
6 #include "mem.h"
7
8 #ifdef TASK
9 #include "Queue.h"
10 #include "SimpleHash.h"
11 #include "task.h"
12 struct SimpleHash * activetasks;
13 struct parameterwrapper * objectqueues[NUMCLASSES];
14
15 int main(int argc, char **argv) {
16   int i;
17   /* Allocate startup object */
18   struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
19   struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc); 
20
21   activetasks=allocateSimpleHash(50);
22
23   /* Set flags */
24   processtasks();
25   flagorand(startupobject,1,0xFFFFFFFF);
26
27   /* Build array of strings */
28
29   startupobject->___parameters___=stringarray;
30
31   for(i=0;i<argc;i++) {
32     int length=strlen(argv[i]);
33     struct ___String___ *newstring=NewString(argv[i],length);
34     ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i]=newstring;
35   }
36   executetasks();
37 }
38
39 void flagorand(void * ptr, int ormask, int andmask) {
40   int flag=((int *)ptr)[1];
41   struct QueueItem *flagptr=(struct QueueItem *)(((int*)ptr)[2]);
42   flag|=ormask;
43   flag&=andmask;
44   ((int*)ptr)[1]=flag;
45   /*Remove from all queues */
46   while(flagptr!=NULL) {
47     struct QueueItem * next=flagptr->nextqueue;
48     removeItem(flagptr->queue, flagptr);
49     flagptr=next;
50   }
51   
52   {
53     struct QueueItem *tmpptr;
54     struct parameterwrapper * parameter=objectqueues[((int *)ptr)[0]];
55     int i;
56     flagptr=NULL;
57     for(i=0;i<parameter->numberofterms;i++) {
58       int andmask=parameter->intarray[i*2];
59       int checkmask=parameter->intarray[i*2+1];
60       if ((flag&andmask)==checkmask) {
61         struct QueueItem * qitem=addNewItem(parameter->queue, ptr);
62         if (flagptr==NULL) {
63           flagptr=qitem;
64           tmpptr=flagptr;
65         } else {
66           tmpptr->nextqueue=qitem;
67           tmpptr=qitem;
68         }
69         SimpleHashadd(activetasks, (int)parameter->task, (int)parameter->task);
70         break;
71       }
72     }
73     ((struct QueueItem **)ptr)[2]=flagptr;
74   }
75 }
76
77 void executetasks() {
78   void * pointerarray[MAXTASKPARAMS];
79   while(1) {
80   newtask:
81     {
82     struct taskdescriptor * task=(struct taskdescriptor *) SimpleHashfirstkey(activetasks);
83     int i;
84     if (task==NULL)
85       break;
86     for(i=0;i<task->numParameters;i++) {
87       struct parameterwrapper * parameter=(struct parameterwrapper *) task->descriptorarray[i]->queue;
88       struct Queue * queue=parameter->queue;
89       if (isEmpty(queue)) {
90         SimpleHashremove(activetasks, (int)task, (int)task);
91         goto newtask;
92       }
93       pointerarray[i]=getTail(queue)->objectptr;
94     }
95     ((void (*) (void **)) task->taskptr)(pointerarray);
96     }
97   }
98 }
99
100 void processtasks() {
101   int i;
102   for(i=0;i<numtasks;i++) {
103     struct taskdescriptor * task=taskarray[i];
104     int j;
105
106     for(j=0;j<task->numParameters;j++) {
107       struct parameterdescriptor *param=task->descriptorarray[j];
108       struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
109       struct parameterwrapper ** ptr=&objectqueues[param->type];
110
111       param->queue=parameter;
112       parameter->queue=createQueue();
113       parameter->numberofterms=param->numberterms;
114       parameter->intarray=param->intarray;
115       parameter->task=task;
116       /* Link new queue in */
117       while(*ptr!=NULL)
118         ptr=&((*ptr)->next);
119       (*ptr)=parameter;
120     }
121   }
122 }
123 #endif
124
125 int ___Object______hashcode____(struct ___Object___ * ___this___) {
126   return (int) ___this___;
127 }
128
129 void ___System______printString____L___String___(struct ___String___ * s) {
130     struct ArrayObject * chararray=s->___string___;
131     int i;
132     for(i=0;i<chararray->___length___;i++) {
133         short s= ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i];
134         putchar(s);
135     }
136 }
137
138 void * allocate_new(int type) {
139   void * v=FREEMALLOC(classsize[type]);
140   *((int *)v)=type;
141   return v;
142 }
143
144 struct ArrayObject * allocate_newarray(int type, int length) {
145   struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
146   v->type=type;
147   v->___length___=length;
148   return v;
149 }
150
151 struct ___String___ * NewString(char *str,int length) {
152   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
153   struct ___String___ * strobj=allocate_new(STRINGTYPE);
154   int i;
155   strobj->___string___=chararray;
156   for(i=0;i<length;i++) {
157     ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i];  }
158   return strobj;
159 }
160
161 void failedboundschk() {
162   printf("Array out of bounds\n");
163   exit(-1);
164 }
165
166 void failednullptr() {
167   printf("Dereferenced a null pointer\n");
168   exit(-1);
169 }