Skip to content

Commit 6e112ef

Browse files
committed
Add readme for the C compiler
1 parent fac8b8c commit 6e112ef

File tree

6 files changed

+95
-11
lines changed

6 files changed

+95
-11
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ in Minecraft 1.12, Command Block Assembly now outputs functions.
1111
Functions greatly increase the speed of execution, so Command Block Assembly
1212
keeps the usage of command blocks to a minimum.
1313

14+
# C Compiler
15+
16+
There is a C compiler that compiles to this assembly language,
17+
read more [here](https://github.com/simon816/Command-Block-Assembly/blob/master/README_C.md).
18+
1419
# The Assembly Language
1520

1621
It is a simple language with instructions similar to that of x86.
@@ -305,9 +310,6 @@ Instead, they evaluate the most recent CMP instruction.
305310
The assembler keeps a reference to the most recent CMP instruction. If a conditional jump instruction
306311
is encountered, it fetches this CMP to decide whether to jump or not.
307312

308-
For almost all use-cases, this should not matter, but it's something to keep in mind and may not be what
309-
one anticipates.
310-
311313
A more accurate conditional jump could be an instruction that takes 3 arguments e.g: `JL left, right, label`.
312314
However writing out the comparison is clunky when performing a succinct multi-jump like this:
313315
```asm

README_C.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
The C Compiler
2+
==============
3+
4+
The compiler takes a C file and converts it to Command Block Assembly instructions,
5+
then feeds that to the assembler to generate the final output.
6+
7+
Almost all language features are supported, including a preprocessor with macros.
8+
9+
10+
There is only one built-in type, `int`. Fundamentally, all data is stored in a scoreboard objective
11+
which is a 32 bit signed integer.
12+
13+
The compiler is invoked by calling `compiler_main.py`.
14+
15+
The parameters are similar to the assembler, with the addition of `--page-size` and `--dump-asm`.
16+
17+
18+
Command line parameters:
19+
```
20+
usage: compiler_main.py [-h] [--world-dir WORLD_DIR] [--namespace NAMESPACE]
21+
[--rem-existing] [--debug] [--stack STACK] [--arg ARG]
22+
[--place-location PLACE_LOCATION] [--enable-sync]
23+
[--page-size PAGE_SIZE] [--dump-asm]
24+
file
25+
26+
positional arguments:
27+
file C File
28+
29+
optional arguments:
30+
-h, --help show this help message and exit
31+
--world-dir WORLD_DIR
32+
World Directory
33+
--namespace NAMESPACE
34+
Function namespace
35+
--rem-existing Remove existing functions in namespace
36+
--debug Enable debug output
37+
--stack STACK Stack size
38+
--arg ARG ASM file arguments
39+
--place-location PLACE_LOCATION
40+
Location to place command blocks
41+
--enable-sync Enable SYNC opcode
42+
--page-size PAGE_SIZE
43+
Memory page size
44+
--dump-asm Dump generated ASM
45+
```
46+
47+
There are some examples in the [examples](https://github.com/simon816/Command-Block-Assembly/tree/master/examples) directory.
48+
49+
The [mclib.h](https://github.com/simon816/Command-Block-Assembly/blob/master/examples/mclib.h) file
50+
contains several useful macros and definitions.

compiler/asm_extensions.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ def __init__(self):
5454
'MOVINDS': self.handle_mov_ind_s
5555
})
5656

57+
def handle_ret(self):
58+
if not self.enable_sync:
59+
# No need to warn here, the compiler is safe with CALL/RET
60+
return
61+
super().handle_ret()
62+
5763
def handle_mov_ind(self, src, s_off, dest, d_off):
5864
"""Move indirect src to indirect dest"""
5965
src, dest = self.get_src_dest(src, dest)

compiler_main.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,14 @@
1111

1212
if __name__ == '__main__':
1313
parser = argparse.ArgumentParser()
14-
parser.add_argument('file', help="ASM File", type=argparse.FileType('r'))
14+
parser.add_argument('file', help="C File", type=argparse.FileType('r'))
1515
parser.add_argument('--world-dir', help="World Directory")
1616
parser.add_argument('--namespace', help="Function namespace", default='c_generated')
1717
parser.add_argument('--rem-existing', help="Remove existing functions in namespace",
1818
action='store_true')
1919
parser.add_argument('--debug', action='store_true', help="Enable debug output")
2020
parser.add_argument('--stack', help="Stack size", type=int, default=8)
2121
parser.add_argument('--arg', help="ASM file arguments", action='append')
22-
parser.add_argument('--jump', help='Output subroutine jump instruction')
2322
parser.add_argument('--place-location', default="~1,~,~1",
2423
help="Location to place command blocks")
2524
parser.add_argument('--enable-sync', help="Enable SYNC opcode", action='store_true')
@@ -69,7 +68,8 @@
6968
print ('/' + setup)
7069
print('== Cleanup command ==')
7170
print('/' + cleanup)
72-
73-
if args.jump:
74-
print('== Jump to %s command ==' % args.jump)
75-
print('/' + assembler.get_sub_jump_command(args.jump).resolve(session.scope))
71+
if 'main' in assembler.subroutines:
72+
print('== Run main() ==')
73+
print('/' + assembler.get_sub_jump_command('main').resolve(session.scope))
74+
else:
75+
print('Cannot output jump: No main() function')

examples/hdd_driver.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ int __hdd_mul;
1717
void memory_seek();
1818

1919
void read_mem() {
20-
CMD(summon armor_stand $arg:mem_loc$ {Tags:["$tag:_mem_ptr"], NoGravity:1b, Marker: 1b});
20+
CMD(summon armor_stand $arg:mem_loc$ {Tags:["$tag:_mem_ptr$"], NoGravity:1b, Marker: 1b});
2121
__hdd_addr = mar;
2222
mbr = 0;
2323
__hdd_addr /= MEM_SIZE_X;
@@ -39,7 +39,7 @@ void read_mem() {
3939
int __mem_temp;
4040

4141
void write_mem() {
42-
CMD(summon armor_stand $arg:mem_loc$ {Tags:["$tag:_mem_ptr"], NoGravity:1b, Marker: 1b});
42+
CMD(summon armor_stand $arg:mem_loc$ {Tags:["$tag:_mem_ptr$"], NoGravity:1b, Marker: 1b});
4343
__hdd_addr = mar;
4444
__hdd_addr /= MEM_SIZE_X;
4545
memory_seek();

examples/mem_test.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include "hdd_driver.c"
2+
3+
int count;
4+
5+
void main() {
6+
count = 0;
7+
while (count != 64) {
8+
mar = count;
9+
mbr = count;
10+
write_mem();
11+
sync;
12+
count += 1;
13+
}
14+
}
15+
16+
17+
void clear() {
18+
count = 0;
19+
mbr = 0;
20+
while (count != 64) {
21+
mar = count;
22+
write_mem();
23+
sync;
24+
count += 1;
25+
}
26+
}

0 commit comments

Comments
 (0)