Skip to content

Commit

Permalink
Merge pull request #16 from fabaindaiz/dev
Browse files Browse the repository at this point in the history
Dev: add compiled code verify with pmars
  • Loading branch information
fabaindaiz authored Oct 20, 2023
2 parents 76dde1f + 0146297 commit b66735f
Show file tree
Hide file tree
Showing 82 changed files with 21,904 additions and 269 deletions.
20 changes: 5 additions & 15 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
# Picked from https://stackoverflow.com/questions/714100/os-detecting-makefile
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
BIN_FORMAT = elf64
CFLAGS ?= -z notext -g
endif
ifeq ($(UNAME_S),Darwin) # for mac
BIN_FORMAT = macho64
CFLAGS ?= -Wl,-no_pie
endif

F = # nothing by default
src = # nothing by default

Expand All @@ -20,7 +9,7 @@ init:
tests:
dune exec execs/run_test.exe -- test '$(F)'

ctest:
ctests:
dune exec execs/run_test.exe -- test '$(F)' -c

compile:
Expand All @@ -32,8 +21,9 @@ compile:
%.exe:
dune build execs/$@

clean: clean-test
rm -Rf _build
clean: clean-tests
dune clean

clean-tests:
find bbctests/ -type f -regex '.*\.\(o\|s\|run\|result\)' -delete
rm -f bbctests/*.s bbctests/*.o bbctests/*.run bbctests/*.result bbctests/*~
rm -rf bbctests/*dSYM
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

This software aims to be an easier way to write and optimize code for corewars. corewars-compiler uses its own mini functional language called RED which can then be compiled into optimized redcode.

work in progress.
#### work in progress.


### Instructions of use
Expand All @@ -22,10 +22,8 @@ To execute the resulting redcode you can use one of these redcode interpreters.
#### TODO

- Create a RED language tutorial
- Test all instructions modifiers and addressing modes
- Improve control flow instructions and conditions

- Reduce duplicate code reduce duplicate code
- Reduce duplicate code and improve compiled redcode

## Acknowledgements

Expand Down
7 changes: 2 additions & 5 deletions dev/analyse.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,17 @@ let analyse_store_arg (arg : arg) (id : string) (place : place) (penv : penv) :

let analyse_store_cond (cond : cond) (id : string) (penv : penv) : penv =
match cond with
| Cond0 -> penv
| Cond1 (_, a) -> (analyse_store_arg a id PB penv)
| Cond2 (_, a1, a2) ->
let penv' = (analyse_store_arg a1 id PA penv) in
(analyse_store_arg a2 id PB penv')

let rec analyse_store_expr (e : tag eexpr) (id : string) (penv : penv) : penv =
match e with
| EPrim2 (_, a1, a2, _) ->
| EPrim2 (_, _, a1, a2, _) ->
let env' = (analyse_store_arg a1 id PA penv) in
(analyse_store_arg a2 id PB env')
| EPrim2m (_, _, a1, a2, _) ->
let env' = (analyse_store_arg a1 id PA penv) in
(analyse_store_arg a2 id PB env')
| EFlow (_, exp, _) -> (analyse_store_expr exp id penv)
| EFlow1 (_, cond, exp, _) ->
let env' = (analyse_store_cond cond id penv) in
(analyse_store_expr exp id env')
Expand Down
39 changes: 19 additions & 20 deletions dev/ast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ type cond2 =
| Clt

type cond =
| Cond0
| Cond1 of cond1 * arg
| Cond2 of cond2 * arg * arg


type imod =
| MNone
| MDef
| MN
| MA
| MB
| MAB
Expand All @@ -57,8 +59,6 @@ type prim2 =
| Jmp
| Spl
| Nop

type prim2m =
| Mov
| Add
| Sub
Expand All @@ -75,10 +75,8 @@ type prim2m =
| Ldp


type flow =
| Repeat

type flow1 =
| Repeat
| If
| While
| DoWhile
Expand All @@ -90,9 +88,7 @@ type flow2 =
type expr =
| Comment of string
| Label of string
| Prim2 of prim2 * arg * arg
| Prim2m of prim2m * imod * arg * arg
| Flow of flow * expr
| Prim2 of prim2 * imod * arg * arg
| Flow1 of flow1 * cond * expr
| Flow2 of flow2 * cond * expr * expr
| Let of string * arg * expr
Expand All @@ -101,9 +97,7 @@ type expr =
type 'a eexpr =
| EComment of string
| ELabel of string * 'a
| EPrim2 of prim2 * arg * arg * 'a
| EPrim2m of prim2m * imod * arg * arg * 'a
| EFlow of flow * 'a eexpr * 'a
| EPrim2 of prim2 * imod * arg * arg * 'a
| EFlow1 of flow1 * cond * 'a eexpr * 'a
| EFlow2 of flow2 * cond * 'a eexpr * 'a eexpr * 'a
| ELet of string * arg * 'a eexpr * 'a
Expand All @@ -119,15 +113,9 @@ let rec tag_expr_help (e : expr) (cur : tag) : (tag eexpr * tag) =
| Label (s) ->
let (next_tag) = (cur + 1) in
(ELabel (s, cur), next_tag)
| Prim2 (op, a1, a2) ->
| Prim2 (op, m, a1, a2) ->
let (next_tag) = (cur + 1) in
(EPrim2 (op, a1, a2, cur), next_tag)
| Prim2m (op, m, a1, a2) ->
let (next_tag) = (cur + 1) in
(EPrim2m (op, m, a1, a2, cur), next_tag)
| Flow (op, expr) ->
let (tag_expr, next_tag) = tag_expr_help expr (cur + 1) in
(EFlow (op, tag_expr, cur), next_tag)
(EPrim2 (op, m, a1, a2, cur), next_tag)
| Flow1 (op, cond, expr) ->
let (tag_expr, next_tag) = tag_expr_help expr (cur + 1) in
(EFlow1 (op, cond, tag_expr, cur), next_tag)
Expand All @@ -151,3 +139,14 @@ let rec tag_expr_help (e : expr) (cur : tag) : (tag eexpr * tag) =

let tag_expr (e : expr) : tag eexpr =
let (tagged, _) = tag_expr_help e 1 in tagged


(* Pretty printing - used by testing framework *)
let string_of_arg(a : arg) : string =
match a with
| ANone -> "0"
| AStore s -> s
| ANum n -> Int.to_string n
| AId s -> s
| ARef (_, n) -> Int.to_string n
| ALab (_, s) -> s
Loading

0 comments on commit b66735f

Please sign in to comment.