Skip to content

Commit 10c7254

Browse files
authored
Modules part 1 (0.13.)
Modules part 1 (0.13.0)
2 parents 1f9fb65 + 7ed9b76 commit 10c7254

33 files changed

+4999
-953
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
.dub
22
docs.json
33
__dummy.html
4-
docs/
54
/cac
65
cac.so
76
cac.dylib
@@ -17,3 +16,5 @@ cac-test-*
1716
/test.cal
1817
/out
1918
/out.*
19+
*.mod
20+
/test*

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ Lua, while keeping its low level features (like direct access to memory).
1414

1515
## Build
1616
```
17-
dub build
17+
dub build --compiler=ldc
1818
```
1919

20+
Compiling with LDC is required because DMD freezes
21+
2022
The compiler executable will be called `cac`
2123

24+
> [!WARNING]
25+
> Compilation may freeze due to a bug in the Digital Mars D compiler. If this happens,
26+
> compile with this command: `dub build --compiler=ldc`
27+
2228
## Run example programs
2329
Make sure you get the standard library
2430

docs/asmmod.md

Lines changed: 238 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,238 @@
1+
# ASMMod file format
2+
ASMMod is a file format used by the Callisto compiler for compiled modules. It contains
3+
function defintions and top level code with their generated assembly
4+
5+
It is made up of sections, each one has a byte describing what kind of section it is:
6+
7+
| Byte | Meaning |
8+
| ---- | -------------------- |
9+
| 0x00 | Top-level code |
10+
| 0x01 | Function definition |
11+
| 0x02 | Import |
12+
| 0x03 | Enable statement |
13+
| 0x04 | Const statement |
14+
| 0x05 | Enum statement |
15+
| 0x06 | Restrict statement |
16+
| 0x07 | Union statement |
17+
| 0x08 | Alias statement |
18+
| 0x09 | Implement statement |
19+
| 0x0A | Let statement |
20+
| 0x0B | Structure |
21+
| 0x0C | BSS section assembly |
22+
| 0x0D | Data section |
23+
| 0x0E | Extern function |
24+
25+
Integers are little endian. Strings are null-terminated.
26+
27+
## Header
28+
| Offset (bytes) | Size (bytes) | Description |
29+
| -------------- | ------------ | ------------------------------------------------------- |
30+
| 0 | 3 | ASCII magic bytes containing "MOD" |
31+
| 3 | 1 | Flags |
32+
| 4 | 8 | Version number, currently 0x0000 |
33+
| 12 | 8 | CPU architecture ID (see below) |
34+
| 20 | 8 | Operating system ID (see below) |
35+
| 28 | 8 | Number of sections |
36+
| 36 | 8 | String containing full path of the original source file |
37+
38+
#### Flags
39+
| Bit | Value |
40+
| --- | ---------------------------------------------------------- |
41+
| 0 | 1 - this module is a stub, 0 - this is a full module |
42+
| 1 | 1 - this is the main module, 0 - it is not the main module |
43+
44+
### CPU architectures
45+
"None" is used for transpiled languages (like when Callisto compiles to Lua)
46+
| ID | Name |
47+
| ------ | --------------- |
48+
| 0x0000 | None |
49+
| 0x0001 | x86_64 |
50+
| 0x0002 | x86 real mode |
51+
| 0x0003 | ARM64 |
52+
| 0x0004 | Uxn |
53+
| 0x0005 | Fox32 |
54+
| 0x0006 | Motorola 68000 |
55+
| 0x0007 | RISC-V |
56+
57+
### Operating systems
58+
| ID | Name |
59+
| ------ | --------------- |
60+
| 0x0000 | None |
61+
| 0x0001 | Linux |
62+
| 0x0002 | macOS |
63+
| 0x0003 | Windows |
64+
| 0x0004 | MS-DOS |
65+
| 0x0005 | FreeBSD |
66+
| 0x0006 | Varvara |
67+
| 0x0007 | Fox32OS |
68+
69+
# Sections
70+
The following tables will include the type byte mentioned before
71+
72+
## Top level code
73+
| Offset (bytes) | Size (bytes) | Description |
74+
| -------------- | ------------ | -------------------------------------------------- |
75+
| 0 | 1 | Type - set to 0x00 |
76+
| 1 | 8 | Number of called functions |
77+
| 9 | ? | Array of strings containing called functions |
78+
| ? | ? | Assembly of top level code |
79+
80+
## Function definition
81+
| Offset (bytes) | Size (bytes) | Description |
82+
| -------------- | ------------ | -------------------------------------------------- |
83+
| 0 | 1 | Type - set to 0x01 |
84+
| 1 | 2 | Flags - see below |
85+
| 3 | 8 | Number of called functions |
86+
| 11 | ? | Array of strings containing called functions |
87+
| ? | ? | Assembly of function (includes label) |
88+
| ? | ? | Function name |
89+
| ? | 8 | Number of parameters |
90+
| ? | 8 | Number of return values |
91+
92+
### Flags
93+
Flags are OR'd together
94+
| Number | Description |
95+
| ------- | -------------------------------------- |
96+
| 1 | Public |
97+
| 2 | Inline (the assembly is callisto code) |
98+
| 4 | Throws errors |
99+
100+
# Import
101+
| Offset (bytes) | Size (bytes) | Description |
102+
| -------------- | ------------ | -------------------------------------------------- |
103+
| 0 | 1 | Type - set to 0x02 |
104+
| 1 | 1 | Boolean - public?
105+
| 2 | ? | String containing name of module |
106+
107+
## Enable statement
108+
| Offset (bytes) | Size (bytes) | Description |
109+
| -------------- | ------------ | -------------------------------------------------- |
110+
| 0 | 1 | Type - set to 0x03 |
111+
| 1 | ? | String containing version to enable |
112+
113+
## Const statement
114+
| Offset (bytes) | Size (bytes) | Description |
115+
| -------------- | ------------ | -------------------------------------------------- |
116+
| 0 | 1 | Type - set to 0x04 |
117+
| 1 | 8 | Const value |
118+
| 9 | ? | String containing const name |
119+
120+
## Enum statement
121+
### Enum header
122+
| Offset (bytes) | Size (bytes) | Description |
123+
| -------------- | ------------ | -------------------------------------------------- |
124+
| 0 | 1 | Type - set to 0x05 |
125+
| 1 | 8 | Number of enum values |
126+
| 9 | ? | Enum name |
127+
| ? | ? | String - enum type |
128+
129+
Enum entries follow this header.
130+
131+
### Enum entry
132+
| Offset (bytes) | Size (bytes) | Description |
133+
| -------------- | ------------ | -------------------------------------------------- |
134+
| 0 | 8 | Enum value |
135+
| 8 | ? | Enum name |
136+
137+
## Restrict statement
138+
| Offset (bytes) | Size (bytes) | Description |
139+
| -------------- | ------------ | -------------------------------------------------- |
140+
| 0 | 1 | Type - set to 0x06 |
141+
| 1 | ? | String containing version to restrict |
142+
143+
## Union staement
144+
| Offset (bytes) | Size (bytes) | Description |
145+
| -------------- | ------------ | -------------------------------------------------- |
146+
| 0 | 1 | Type - set to 0x07 |
147+
| 1 | ? | Union name |
148+
| ? | 8 | Number of types in union |
149+
| ? | ? | String array with type names |
150+
151+
## Alias statement
152+
| Offset (bytes) | Size (bytes) | Description |
153+
| -------------- | ------------ | -------------------------------------------------- |
154+
| 0 | 1 | Type - set to 0x08 |
155+
| 1 | ? | Original type name |
156+
| ? | ? | New type name |
157+
158+
## Implement statement
159+
| Offset (bytes) | Size (bytes) | Description |
160+
| -------------- | ------------ | -------------------------------------------------- |
161+
| 0 | 1 | Type - set to 0x09 |
162+
| 1 | ? | Type name |
163+
| ? | ? | Method name |
164+
| ? | 8 | Number of called functions |
165+
| ? | ? | Array of strings containing called functions |
166+
| ? | ? | Assembly of compiled implement statement |
167+
168+
## Let statement
169+
| Offset (bytes) | Size (bytes) | Description |
170+
| -------------- | ------------ | -------------------------------------------------- |
171+
| 0 | 1 | Type - set to 0x0A |
172+
| 1 | 1 | Boolean - is this an array? |
173+
| 2 | 8 | Array size |
174+
| 10 | 1 | Boolean - is this a pointer? |
175+
| 10 | ? | Type |
176+
| ? | ? | Variable name |
177+
178+
## Struct
179+
| Offset (bytes) | Size (bytes) | Description |
180+
| -------------- | ------------ | -------------------------------------------------- |
181+
| 0 | 1 | Type - set to 0x0B |
182+
| 1 | ? | Structure name |
183+
| ? | ? | String - parent structure, empty if none |
184+
| ? | 8 | Number of structure entries |
185+
| ? | ? | Structure entries |
186+
187+
### Structure entries
188+
| Offset (bytes) | Size (bytes) | Description |
189+
| -------------- | ------------ | -------------------------------------------------- |
190+
| 0 | 1 | Bool - is pointer? |
191+
| 1 | ? | Type name |
192+
| ? | 1 | Bool - is array? |
193+
| ? | 8 | Array length, if applicable |
194+
| ? | 8 | Offset in structure |
195+
| ? | ? | Member name |
196+
197+
## BSS Section assembly
198+
| Offset (bytes) | Size (bytes) | Description |
199+
| -------------- | ------------ | -------------------------------------------------- |
200+
| 0 | 1 | Type - set to 0x0C |
201+
| 1 | ? | Assembly |
202+
203+
## Data section assembly
204+
| Offset (bytes) | Size (bytes) | Description |
205+
| -------------- | ------------ | -------------------------------------------------- |
206+
| 0 | 1 | Type - set to 0x0D |
207+
| 1 | ? | Assembly |
208+
209+
## Extern function
210+
| Offset (bytes) | Size (bytes) | Description |
211+
| -------------- | ------------ | -------------------------------------------------- |
212+
| 0 | 1 | Type - set to 0x0E |
213+
| 1 | 1 | Extern type - see below table |
214+
| 2 | 4 | Number of return values |
215+
| 4 | 4 | Number of parameters |
216+
| 8 | ? | String - symbol name |
217+
| ? | ? | String - function name |
218+
219+
After this, there are the return values, and then the parameters. Both have this
220+
format per value:
221+
222+
| Offset (bytes) | Size (bytes) | Description |
223+
| -------------- | ------------ | -------------------------------------------------- |
224+
| 0 | 1 | Bool - is pointer? |
225+
| 1 | ? | Name of type |
226+
227+
Extern types:
228+
229+
| Value | Name |
230+
| ----- | ----------- |
231+
| 0 | Callisto |
232+
| 1 | Raw |
233+
| 2 | C |
234+
235+
Callisto externs are currently due to be replaced with a better version, and raw
236+
externs are going to be removed. Do not use either.
237+
238+
C externs must have either no return values (void) or one return value.

editors/micro_callisto.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ rules:
77
- statement: "\\b(func|end|begin|asm|include|inline|if|then|elseif|else|while|do)\\b"
88
- statement: "\\b(let|enable|requires|struct|version|return|const|enum|restrict)\\b"
99
- statement: "\\b(continue|break|union|alias|overwrite|error|extern|call|raw)\\b"
10-
- statement: "\\b(implement|as|try|catch|throw|unsafe|man|ptr|anon)\\b"
11-
- type: "\\b(addr|void|u8|i8|u16|i16|u32|i32|u64|i64|size|usize|icell|cell|array|bool)\\b"
12-
- type: "\\b[A-Z]+[a-zA-Z_0-9]*[a-z]*[a-zA-Z_0-9]*\\b"
10+
- statement: "\\b(implement|as|try|catch|throw|unsafe|man|ptr|import|module main)\\b"
11+
- statement: "\\b(public)\\b"
12+
- type: "\\b(addr|void|u8|i8|u16|i16|u32|i32|u64|i64|size|usize|cell|array)\\b"
13+
- type: "\\b[A-Z]+[a-zA-Z_0-9]*[a-z]+[a-zA-Z_0-9]*\\b"
1314

1415
- constant.string:
1516
start: "\""

examples/factorial.cal

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include "cores/select.cal"
2+
include "std/io.cal"
3+
4+
func factorial cell n -> cell res begin
5+
if n 2 < then 1 end
6+
else n n 1 - factorial * end
7+
end

examples/initDeinit.cal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ end
1212

1313
implement meow deinit
1414
"meow end\n" print_str
15+
drop
1516
end
1617

1718
let meow myMeow

examples/stars.cal

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
include "cores/select.cal"
2+
include "std/io.cal"
23

34
let cell x
4-
0 -> x
55
let cell y
6-
0 -> y
76

87
while y 20 < do
98
while x 40 < do
@@ -15,7 +14,7 @@ while y 20 < do
1514

1615
x 1 + -> x
1716
end
18-
13 print_ch 10 print_ch
17+
new_line
1918
y 1 + -> y
2019
0 -> x
2120
end

examples/writeFile.cal

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,4 @@ end
1212
let Array str
1313
"meow\n" &str a<
1414

15-
&file
16-
&str Array.elements + @
17-
&str Array.length + @ file!
15+
&file str.elements str.length file!

0 commit comments

Comments
 (0)