checked in bug fixes...
[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     while(parameter!=NULL) {
58       for(i=0;i<parameter->numberofterms;i++) {
59         int andmask=parameter->intarray[i*2];
60         int checkmask=parameter->intarray[i*2+1];
61         if ((flag&andmask)==checkmask) {
62           struct QueueItem * qitem=addNewItem(parameter->queue, ptr);
63           if (flagptr==NULL) {
64             flagptr=qitem;
65             tmpptr=flagptr;
66           } else {
67             tmpptr->nextqueue=qitem;
68             tmpptr=qitem;
69           }
70           SimpleHashadd(activetasks, (int)parameter->task, (int)parameter->task);
71           break;
72         }
73       }
74       parameter=parameter->next;
75     }
76     ((struct QueueItem **)ptr)[2]=flagptr;
77   }
78 }
79
80 void executetasks() {
81   void * pointerarray[MAXTASKPARAMS];
82   newtask:
83   while(SimpleHashcountset(activetasks)!=0) {
84     struct taskdescriptor * task=(struct taskdescriptor *) SimpleHashfirstkey(activetasks);
85     int i;
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 void processtasks() {
100   int i;
101   for(i=0;i<numtasks;i++) {
102     struct taskdescriptor * task=taskarray[i];
103     int j;
104
105     for(j=0;j<task->numParameters;j++) {
106       struct parameterdescriptor *param=task->descriptorarray[j];
107       struct parameterwrapper * parameter=RUNMALLOC(sizeof(struct parameterwrapper));
108       struct parameterwrapper ** ptr=&objectqueues[param->type];
109
110       param->queue=parameter;
111       parameter->queue=createQueue();
112       parameter->numberofterms=param->numberterms;
113       parameter->intarray=param->intarray;
114       parameter->task=task;
115       /* Link new queue in */
116       while((*ptr)!=NULL)
117         ptr=&((*ptr)->next);
118       (*ptr)=parameter;
119     }
120   }
121 }
122 #endif
123
124 int ___Object______hashcode____(struct ___Object___ * ___this___) {
125   return (int) ___this___;
126 }
127
128 void ___System______printString____L___String___(struct ___String___ * s) {
129     struct ArrayObject * chararray=s->___string___;
130     int i;
131     for(i=0;i<chararray->___length___;i++) {
132         short s= ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i];
133         putchar(s);
134     }
135 }
136
137 void * allocate_new(int type) {
138   void * v=FREEMALLOC(classsize[type]);
139   *((int *)v)=type;
140   return v;
141 }
142
143 struct ArrayObject * allocate_newarray(int type, int length) {
144   struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
145   v->type=type;
146   v->___length___=length;
147   return v;
148 }
149
150 struct ___String___ * NewString(char *str,int length) {
151   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
152   struct ___String___ * strobj=allocate_new(STRINGTYPE);
153   int i;
154   strobj->___string___=chararray;
155   for(i=0;i<length;i++) {
156     ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i];  }
157   return strobj;
158 }
159
160 void failedboundschk() {
161   printf("Array out of bounds\n");
162   exit(-1);
163 }
164
165 void failednullptr() {
166   printf("Dereferenced a null pointer\n");
167   exit(-1);
168 }