Mercurial > repos > rhope
annotate runtime/builtin.c @ 86:a163250b8885
Update fib example to reflect language changes
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 30 Jul 2010 23:49:39 +0000 |
parents | a844c623c7df |
children | e09c2d1d6d5b |
rev | line source |
---|---|
7
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 #include "builtin.h" |
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 #include "object.h" |
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 #include "integer.h" |
12
31f8182f3433
Finished fib test and did some small work on the c backend
Mike Pavone <pavone@retrodev.com>
parents:
7
diff
changeset
|
4 #include "bool.h" |
7
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 #include <stddef.h> |
66
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
6 #include <stdio.h> |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
7 #include <stdlib.h> |
52
079200bc3e75
String literals almost working. Print moved out of C runtime.
Mike Pavone <pavone@retrodev.com>
parents:
50
diff
changeset
|
8 #include <string.h> |
7
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 |
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 void register_builtin_type(uint32_t type) |
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 { |
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 blueprint * bp; |
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 switch(type) |
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 { |
36
495dddadd058
User defined types work in the compiler now
Mike Pavone <pavone@retrodev.com>
parents:
30
diff
changeset
|
15 case TYPE_BLUEPRINT: |
495dddadd058
User defined types work in the compiler now
Mike Pavone <pavone@retrodev.com>
parents:
30
diff
changeset
|
16 bp = register_type_byid(TYPE_BLUEPRINT, sizeof(blueprint *), NULL, NULL, NULL); |
495dddadd058
User defined types work in the compiler now
Mike Pavone <pavone@retrodev.com>
parents:
30
diff
changeset
|
17 break; |
7
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 } |
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 } |
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 |
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 void register_builtin_types() |
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 { |
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 uint32_t i; |
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 for(i = 0; i < TYPE_FIRST_USER; ++i) |
d61550e2c001
Added current work on new runtime
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 register_builtin_type(i); |
66
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
26 } |
50
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
27 object * make_Int64(int64_t val) |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
28 { |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
29 t_Int64 * obj; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
30 object * ret = new_object(TYPE_INT64); |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
31 obj = (t_Int64 *)ret; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
32 obj->Num = val; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
33 return ret; |
66
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
34 } |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
35 |
50
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
36 |
48
a24eb366195c
Fixed some bugs introduced in previous commit and moved definition of integer methods out of runtime and into the compiler
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
37 object * make_Int32(int32_t val) |
a24eb366195c
Fixed some bugs introduced in previous commit and moved definition of integer methods out of runtime and into the compiler
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
38 { |
a24eb366195c
Fixed some bugs introduced in previous commit and moved definition of integer methods out of runtime and into the compiler
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
39 t_Int32 * obj; |
a24eb366195c
Fixed some bugs introduced in previous commit and moved definition of integer methods out of runtime and into the compiler
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
40 object * ret = new_object(TYPE_INT32); |
a24eb366195c
Fixed some bugs introduced in previous commit and moved definition of integer methods out of runtime and into the compiler
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
41 obj = (t_Int32 *)ret; |
a24eb366195c
Fixed some bugs introduced in previous commit and moved definition of integer methods out of runtime and into the compiler
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
42 obj->Num = val; |
a24eb366195c
Fixed some bugs introduced in previous commit and moved definition of integer methods out of runtime and into the compiler
Mike Pavone <pavone@retrodev.com>
parents:
37
diff
changeset
|
43 return ret; |
66
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
44 } |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
45 |
50
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
46 object * make_Int16(int16_t val) |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
47 { |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
48 t_Int16 * obj; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
49 object * ret = new_object(TYPE_INT16); |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
50 obj = (t_Int16 *)ret; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
51 obj->Num = val; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
52 return ret; |
66
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
53 } |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
54 |
50
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
55 object * make_Int8(int8_t val) |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
56 { |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
57 t_Int8 * obj; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
58 object * ret = new_object(TYPE_INT8); |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
59 obj = (t_Int8 *)ret; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
60 obj->Num = val; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
61 return ret; |
66
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
62 } |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
63 |
50
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
64 object * make_UInt64(uint64_t val) |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
65 { |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
66 t_UInt64 * obj; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
67 object * ret = new_object(TYPE_UINT64); |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
68 obj = (t_UInt64 *)ret; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
69 obj->Num = val; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
70 return ret; |
66
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
71 } |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
72 |
50
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
73 object * make_UInt32(uint32_t val) |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
74 { |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
75 t_UInt32 * obj; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
76 object * ret = new_object(TYPE_UINT32); |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
77 obj = (t_UInt32 *)ret; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
78 obj->Num = val; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
79 return ret; |
66
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
80 } |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
81 |
50
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
82 object * make_UInt16(uint16_t val) |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
83 { |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
84 t_UInt16 * obj; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
85 object * ret = new_object(TYPE_UINT16); |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
86 obj = (t_UInt16 *)ret; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
87 obj->Num = val; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
88 return ret; |
66
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
89 } |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
90 |
50
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
91 object * make_UInt8(uint8_t val) |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
92 { |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
93 t_UInt8 * obj; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
94 object * ret = new_object(TYPE_UINT8); |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
95 obj = (t_UInt8 *)ret; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
96 obj->Num = val; |
689fb73e7612
Add support for various integer literals and add support for raw pointers to objects and raw pointers to arrays in the FFI
Mike Pavone <pavone@retrodev.com>
parents:
48
diff
changeset
|
97 return ret; |
66
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
98 } |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
99 |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
100 object * make_String(char * text) |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
101 { |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
102 object * params[1]; |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
103 t_Array * arr = (t_Array *)_internal_array_allocnaked(strlen(text), make_Blueprint(TYPE_UINT8)); |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
104 arr->payload.Length = arr->payload.Storage; |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
105 memcpy(((char *)arr) + sizeof(t_Array), text, arr->payload.Length); |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
106 |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
107 params[0] = (object *)arr; |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
108 rhope(FUNC_String, params, 1, 1); |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
109 |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
110 return params[0]; |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
111 } |
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
112 |
72
f7bcf3db1342
Add =@String, fix bool bug
Mike Pavone <pavone@retrodev.com>
parents:
66
diff
changeset
|
113 object * make_Bool(int32_t val) |
f7bcf3db1342
Add =@String, fix bool bug
Mike Pavone <pavone@retrodev.com>
parents:
66
diff
changeset
|
114 { |
f7bcf3db1342
Add =@String, fix bool bug
Mike Pavone <pavone@retrodev.com>
parents:
66
diff
changeset
|
115 t_Boolean * b = (t_Boolean *)new_object(TYPE_BOOLEAN); |
f7bcf3db1342
Add =@String, fix bool bug
Mike Pavone <pavone@retrodev.com>
parents:
66
diff
changeset
|
116 b->Val = val != 0; |
f7bcf3db1342
Add =@String, fix bool bug
Mike Pavone <pavone@retrodev.com>
parents:
66
diff
changeset
|
117 return (object*)b; |
74
a844c623c7df
Add support for Worker type
Mike Pavone <pavone@retrodev.com>
parents:
72
diff
changeset
|
118 } |
a844c623c7df
Add support for Worker type
Mike Pavone <pavone@retrodev.com>
parents:
72
diff
changeset
|
119 |
a844c623c7df
Add support for Worker type
Mike Pavone <pavone@retrodev.com>
parents:
72
diff
changeset
|
120 object * make_Worker(int32_t index, int16_t initialsize, int16_t initialcount) |
a844c623c7df
Add support for Worker type
Mike Pavone <pavone@retrodev.com>
parents:
72
diff
changeset
|
121 { |
a844c623c7df
Add support for Worker type
Mike Pavone <pavone@retrodev.com>
parents:
72
diff
changeset
|
122 t_Worker * worker = (t_Worker *)_internal_worker_alloc(initialsize); |
a844c623c7df
Add support for Worker type
Mike Pavone <pavone@retrodev.com>
parents:
72
diff
changeset
|
123 worker->payload.Index = index; |
a844c623c7df
Add support for Worker type
Mike Pavone <pavone@retrodev.com>
parents:
72
diff
changeset
|
124 worker->payload.Count = initialcount; |
a844c623c7df
Add support for Worker type
Mike Pavone <pavone@retrodev.com>
parents:
72
diff
changeset
|
125 return (object *)worker; |
72
f7bcf3db1342
Add =@String, fix bool bug
Mike Pavone <pavone@retrodev.com>
parents:
66
diff
changeset
|
126 } |
66
d4b44ae2e34a
New variant of C backend works now
Mike Pavone <pavone@retrodev.com>
parents:
63
diff
changeset
|
127 |