c91890a9222ab160cd95a20ad04b53aa46dd6c5d
[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 #ifdef BOEHM_GC
7 #include "gc.h"
8 #define FREEMALLOC(x) GC_malloc(x)
9 #else
10 #define FREEMALLOC(x) calloc(1,x)
11 #endif
12
13 #ifdef TASK
14 int main(int argc, char **argv) {
15   int i;
16   /* Allocate startup object */
17   struct ___StartupObject___ *startupobject=(struct ___StartupObject___*) allocate_new(STARTUPTYPE);
18
19   /* Build array of strings */
20   struct ArrayObject * stringarray=allocate_newarray(STRINGARRAYTYPE, argc); 
21   startupobject->___parameters___=stringarray;
22
23   for(i=0;i<argc;i++) {
24     int length=strlen(argv[i]);
25     struct ___String___ *newstring=NewString(argv[i],length);
26     ((void **)(((char *)& stringarray->___length___)+sizeof(int)))[i]=newstring;  
27   }
28
29
30 }
31 #endif
32
33 int ___Object______hashcode____(struct ___Object___ * ___this___) {
34   return (int) ___this___;
35 }
36
37 void ___System______printString____L___String___(struct ___String___ * s) {
38     struct ArrayObject * chararray=s->___string___;
39     int i;
40     for(i=0;i<chararray->___length___;i++) {
41         short s= ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i];
42         putchar(s);
43     }
44 }
45
46 void * allocate_new(int type) {
47   void * v=FREEMALLOC(classsize[type]);
48   *((int *)v)=type;
49   return v;
50 }
51
52 struct ArrayObject * allocate_newarray(int type, int length) {
53   struct ArrayObject * v=FREEMALLOC(sizeof(struct ArrayObject)+length*classsize[type]);
54   v->type=type;
55   v->___length___=length;
56   return v;
57 }
58
59 struct ___String___ * NewString(char *str,int length) {
60   struct ArrayObject * chararray=allocate_newarray(CHARARRAYTYPE, length);
61   struct ___String___ * strobj=allocate_new(STRINGTYPE);
62   int i;
63   strobj->___string___=chararray;
64   for(i=0;i<length;i++) {
65     ((short *)(((char *)& chararray->___length___)+sizeof(int)))[i]=(short)str[i];  }
66   return strobj;
67 }
68
69 void failedboundschk() {
70   printf("Array out of bounds\n");
71   exit(-1);
72 }
73
74 void failednullptr() {
75   printf("Dereferenced a null pointer\n");
76   exit(-1);
77 }