Skip to content

Commit b7de73b

Browse files
committed
Merge branch 'command-ir'
2 parents 5d25485 + f62a011 commit b7de73b

40 files changed

+7994
-1228
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ env/
44

55
# License incompatible
66
compiler/parser_gen.py
7+
cmd_ir/parser_gen.py
78

README.md

Lines changed: 15 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ void main() {
4141
}
4242
```
4343

44+
# Command IR
45+
46+
A new intermediate representation has been developed that the assembly language builds on top of.
47+
To find out more see [Command IR](https://github.com/simon816/Command-Block-Assembly/wiki/Command-IR).
48+
4449
# The Assembly Language
4550

4651
It is a simple language with instructions similar to that of x86.
@@ -230,10 +235,10 @@ The assembler is invoked by calling `main.py`.
230235

231236
Command line parameters:
232237
```
233-
usage: main.py [-h] [--world-dir WORLD_DIR] [--as_zip] [--namespace NAMESPACE]
234-
[--rem-existing] [--debug] [--stack STACK] [--arg ARG]
235-
[--jump JUMP] [--place-location PLACE_LOCATION] [--enable-sync]
236-
[--setup-on-load] [--spawn-location SPAWN_LOCATION]
238+
usage: main.py [-h] [--world-dir WORLD_DIR] [--as-zip] [--namespace NAMESPACE]
239+
[--rem-existing] [--debug] [--dump-ir] [--gen-cleanup]
240+
[--jump JUMP] --place-location PLACE_LOCATION [--setup-on-load]
241+
[--spawn-location SPAWN_LOCATION]
237242
[--pack-description PACK_DESCRIPTION]
238243
file
239244
@@ -244,17 +249,16 @@ optional arguments:
244249
-h, --help show this help message and exit
245250
--world-dir WORLD_DIR
246251
World Directory
247-
--as_zip Write datapack as zip file
252+
--as-zip Write datapack as zip file
248253
--namespace NAMESPACE
249254
Function namespace
250255
--rem-existing Remove existing functions in namespace
251256
--debug Enable debug output
252-
--stack STACK Stack size
253-
--arg ARG ASM file arguments
257+
--dump-ir Dump Command IR output
258+
--gen-cleanup Generate cleanup function
254259
--jump JUMP Output subroutine jump instruction
255260
--place-location PLACE_LOCATION
256261
Location to place command blocks
257-
--enable-sync Enable SYNC opcode
258262
--setup-on-load Run setup on minecraft:load
259263
--spawn-location SPAWN_LOCATION
260264
Location to spawn hidden armor stand
@@ -267,21 +271,9 @@ Notes:
267271
If `--world-dir` is not provided, no functions are written.
268272
This can be useful in combination with `--debug`.
269273

270-
`--place-location` is where to start laying out command blocks, should they be needed.
271-
Defaults to `~1,~,~1`
274+
`--place-location` is where to place a utility command block. It has to be an absolute position e.g. '0,56,0'
272275

273-
`--arg` is used to pass values in to a program that get replaced in the output. They are currently
274-
only applicable in CMD and TEST instructions.
275-
e.g.
276-
```asm
277-
main: CMD say Hello $arg:name$! This was generated on $arg:date$
278-
```
279-
Running `python main.py test.asm --debug --arg "name=Simon" --arg "date=24/10/2017"`
280-
produces:
281-
```
282-
Function sub_main
283-
say Hello Simon! This was generated on 24/10/2017
284-
```
276+
You may want `--gen-cleanup` which creates a function to remove all scoreboard objectives and the global entity.
285277

286278
### Running a program
287279

@@ -316,45 +308,14 @@ The value is then WORD_SIZE bits in the y axis. i.e. for an 8-bit word, y=0 is t
316308
`hdd_driver.asm` is a library file, and exports the `read_mem`, `write_mem` subroutines along with
317309
its constants.
318310

319-
The location where the memory region is defined must be passed in to the assembler:
320-
`--arg "mem_loc=0 0 0"`
311+
The location where the memory region is hardcoded to be `100 60 100`.
321312

322313
#### `mem_test.asm`
323314

324315
A simple test of the hdd_driver library. See description at the top of the file.
325316

326317
# Issues and nuances
327318

328-
## The SYNC instruction (and how it affects CALL and RET)
329-
330-
Command Block Assembly is designed to produce fast and efficient functions,
331-
avoiding expensive operations wherever possible.
332-
As such, some features are optimized unless it is not possible.
333-
334-
By default, a CALL instruction will add a `/function` command to run the subroutine.
335-
This means that once the function finishes, execution continues at the next command.
336-
This behaviour is the anticipated use of CALL, however the implication is that RET has no effect.
337-
338-
Originally, there was going to be an _implied_ return after a subroutine. i.e. always return to caller if CALL
339-
is ran.
340-
But the RET instruction is required for SYNC to work, so it was added.
341-
342-
As stated in the instruction description, SYNC effectively "pauses" the current execution until one tick later.
343-
344-
The current calling stack (nested `/function` calls) will always return to the caller and continue on the next line.
345-
SYNC must nullify the next call so the calling stack returns and the game runs a tick.
346-
However, this is not possible with CALL's guarantee to return to caller.
347-
348-
Therefore, CALL must push the address of the next instruction onto the stack, letting RET pop it off later.
349-
With that, it is now possible to cause an interrupt between CALL and the subsequent RET.
350-
This is how you would expect CALL and RET to function, but doing so is less efficient for the common use-case.
351-
352-
By default, SYNC is disabled. The use of RET prints a warning to stderr saying how it doesn't have any effect.
353-
If the program is anticipated to use SYNC, you should defensively use RET anyway. Just keep in mind
354-
how the optimization works.
355-
356-
To use SYNC, enable it with `--enable-sync`.
357-
358319
## CMP and jumping
359320

360321
Due to there being no concept of a "status" register, jump instructions don't check flags of any sort.

README_C.md

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ Command line parameters:
1616
```
1717
usage: compiler_main.py [-h] [-E] [-S] [--world-dir WORLD_DIR] [--as-zip]
1818
[--namespace NAMESPACE] [--rem-existing] [--debug]
19-
[--stack STACK] [--arg ARG]
20-
[--place-location PLACE_LOCATION] [--enable-sync]
21-
[--page-size PAGE_SIZE] [--setup-on-load]
19+
[--dump-ir] [--gen-cleanup] --place-location
20+
PLACE_LOCATION [--page-size PAGE_SIZE]
2221
[--spawn-location SPAWN_LOCATION]
2322
[--pack-description PACK_DESCRIPTION]
24-
[--extern EXTERN]
2523
file
2624
2725
positional arguments:
@@ -38,19 +36,16 @@ optional arguments:
3836
Function namespace
3937
--rem-existing Remove existing functions in namespace
4038
--debug Enable debug output
41-
--stack STACK Stack size
42-
--arg ARG ASM file arguments
39+
--dump-ir Dump Command IR output
40+
--gen-cleanup Generate cleanup function
4341
--place-location PLACE_LOCATION
4442
Location to place command blocks
45-
--enable-sync Enable SYNC opcode
4643
--page-size PAGE_SIZE
4744
Memory page size
48-
--setup-on-load Run setup on minecraft:load
4945
--spawn-location SPAWN_LOCATION
5046
Location to spawn hidden armor stand
5147
--pack-description PACK_DESCRIPTION
5248
Datapack description
53-
--extern EXTERN Specify external symbol
5449
```
5550

5651
You will need to generate the standalone parser (from [Lark](https://github.com/lark-parser/lark)) using the `./compiler/rebuild-grammar.sh` script.

0 commit comments

Comments
 (0)