diff options
Diffstat (limited to 'asm')
-rw-r--r-- | asm/asm.ml | 1 | ||||
-rw-r--r-- | asm/asmlex.mll | 1 | ||||
-rw-r--r-- | asm/asmpars.mly | 23 | ||||
-rw-r--r-- | asm/assembler.ml | 6 |
4 files changed, 21 insertions, 10 deletions
@@ -58,6 +58,7 @@ type instr = | Lra of imm | Byte of int | Word of int + | Wlab of string | Hlt module Imap = Map.Make(String) diff --git a/asm/asmlex.mll b/asm/asmlex.mll index 7459966..1f598e9 100644 --- a/asm/asmlex.mll +++ b/asm/asmlex.mll @@ -108,6 +108,7 @@ rule token = parse | '(' { LP } | ')' { RP } | '"' { str [] lexbuf } + |';' { SEMIC } and str acc = parse | "\\\\" { str ('\\' :: acc) lexbuf } diff --git a/asm/asmpars.mly b/asm/asmpars.mly index 4850c55..258396e 100644 --- a/asm/asmpars.mly +++ b/asm/asmpars.mly @@ -23,17 +23,24 @@ let li u r = function | Imm i -> - let c = - if u then i < 1 lsl 8 - else i >= -(1 lsl 7) && i < 1 lsl 7 in - if c then (add pc 2; [Lilz (r,Imm i)]) - else (add pc 4; [Lilz (r,Imm (i land 0x00FF)); Liu (r,Imm ((i land 0xFF00) lsr 8))]) + if u then + if i < 1 lsl 8 then (add pc 2; [Lilz (r,Imm i)]) + else (add pc 4; [Lil (r,Imm (i land 0x00FF)); Liu (r,Imm ((i land 0xFF00) lsr 8))]) + else if i >=0 && i < 1 lsl 7 then + (add pc 2; [Lilz (r,Imm i)]) + else if i < 0 && i >= - ( 1 lsl 7 ) then + (add pc 4; [Lil (r,Imm i); Liu (r,Imm 0xFF)]) + else (add pc 4; [Lil (r,Imm (i land 0x00FF)); Liu (r,Imm ((i land 0xFF00) asr 8))]) | Lab id -> add pc 4; [Lilz (r,Lab id); Liu (r,Lab id)] + + let itw = function + | Imm i -> Word i + | Lab l -> Wlab l %} -%token EOF,COLON,TEXT,DATA,BYTE,WORD,MINUS,MOVE,JZ,JNZ,LP,RP,HLT,ASCII +%token EOF,COLON,TEXT,DATA,BYTE,WORD,MINUS,MOVE,JZ,JNZ,LP,RP,HLT,ASCII,SEMIC %token POP,PUSH,INCRI,SHI,JJ,JAL,JR,JALR,LW,SW,LB,SB,NOT,LIL,LILZ,LIU,LIUZ,LRA,LI %token<Asm.reg> REG %token<Asm.fmt_r> ROP,RIOP @@ -127,8 +134,8 @@ _instr: add pc 2; l @ [R (Jner,5,r,0)] } | POP r=REG { add pc 4; [Lw (r,7,0); Incri (7,2)] } | PUSH r=REG { add pc 4; [Incri (7,-2); Sw (r,7,0)] } - | BYTE bs=int* { List.map (fun b -> add pc 1; Byte b) bs } - | WORD ws=int* { List.map (fun w -> add pc 2; Word w) ws } + | BYTE bs=int+ SEMIC { List.map (fun b -> add pc 1; Byte b) bs } + | WORD ws=imm+ SEMIC { List.map (fun w -> add pc 2; itw w) ws } | HLT { add pc 2; [Hlt] } | ASCII s=STR { List.map (fun c -> add pc 1; Byte (Char.code c)) s } diff --git a/asm/assembler.ml b/asm/assembler.ml index 8c8ff97..35395e0 100644 --- a/asm/assembler.ml +++ b/asm/assembler.ml @@ -116,7 +116,8 @@ let print_program p = sprintf "liuz %s %s" (rts r) (its i) | Hlt -> (0b01111 lsl 11),"hlt" | Word w -> (byte 16 w), "" - | Byte b -> byte 8 b, "" in + | Byte b -> byte 8 b, "" + | Wlab l -> fst (Imap.find l p.lbls), l in let n = List.fold_left (fun n i -> size i + n) 0 p.text in let rev_lbls = Array.make n "" in Imap.iter (fun l (v,t) -> @@ -133,7 +134,8 @@ let print_program p = else printf "\n"; pc := !pc + 2 ) else ( - printf "%s\n" (bts w) ) in + printf "%s\n" (bts w); + incr pc ) in printf "%d %d\n" (n) 8; List.iter f p.text; printf "\n" |