Mercurial > repos > tabletprog
annotate modules/bytearray.tp @ 251:2557ce4e671f
Fix a couple of compiler bugs. topenv was getting initialized in multiple places. This resulted in multiple copies of modules getting created which caused problems for macro expansion. Additionally, arguments were not being marked as declared during code generation so assigning to an argument that was not closed over generated invalid C code.
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 11 Apr 2014 22:29:32 -0700 |
parents | 270d31c6c4cd |
children | fb922651db29 |
rev | line source |
---|---|
178
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 #{ |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 includeSystemHeader: "unistd.h" |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 includeSystemHeader: "sys/mman.h" |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 executable <- :size { |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 buf <- #{ |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 llProperty: bytes withType: uint32_t |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 llProperty: buffer withType: (void ptr) |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 llMessage: _init withVars: { |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 sz <- obj_int32 ptr |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 } andCode: :sz { |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 bytes <- sz num |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 buffer <- sbrk: bytes |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 mprotect: buffer bytes (PROT_READ or PROT_WRITE or PROT_EXEC) |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 self |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 } |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 llMessage: set withVars: { |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 offset <- obj_int32 ptr |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 newval <- obj_uint8 ptr |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 } andCode: :offset newval { |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 (buffer castTo: (uint8_t ptr)) set: (offset num) (newval num) |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 self |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 } |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 llMessage: get withVars: { |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 offset <- obj_int32 ptr |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 ret <- obj_uint8 ptr |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 } andCode: :offset { |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 ret <- make_object: (addr_of: obj_uint8_meta) NULL 0 |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 ret num!: ((buffer castTo: (uint8_t ptr)) get: (offset num)) |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 ret |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 } |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 llMessage: run withVars: { |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 fun <- uint64_t funptr |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 funret <- obj_uint64 ptr |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 } andCode: { |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 funret <- make_object: (addr_of: obj_uint64_meta) NULL 0 |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 fun <- buffer |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 funret num!: ( fun: ) |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 funret |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 } |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 llMessage: runWithArg withVars: { |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 fun <- uint64_t funptr: uint64_t |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 funret <- obj_uint64 ptr |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 arg <- obj_uint64 ptr |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 } andCode: :arg { |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 fun <- buffer |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 funret <- make_object: (addr_of: obj_uint64_meta) NULL 0 |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 funret num!: ( fun: (arg num) ) |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 funret |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 } |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 } |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 buf _init |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 } |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 executableFromBytes <- :bytes { |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 totalSize <- bytes fold: 0 with: :acc el { |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 acc + (el length) |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 } |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 ba <- executable: totalSize |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 bytes fold: 0 with: :idx el { |
180
270d31c6c4cd
Add support for jmps and labels in x86 module
Mike Pavone <pavone@retrodev.com>
parents:
178
diff
changeset
|
62 el flattenTo: ba at: idx |
178
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 } |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 ba |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 } |
e823e104b845
Add initial work on bytearrays with support for allocating executable memory
Mike Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 } |