source: Deliverables/D4.1/Pretty.ml @ 529

Last change on this file since 529 was 454, checked in by sacerdot, 9 years ago

CSC + Nicolas + Dominic:

1) back-porting of changes by Nicolas from the compiler
2) new file ASMCosts to compute the cost of labels
3) several changes here and there to implement 2)

File size: 4.0 KB
Line 
1open BitVectors;;
2open ASM;;
3
4let pp_arg =
5 function
6    `A -> "A"
7  | `B -> "B"
8  | `C -> "C"
9  | `DPTR -> "DPTR"
10  | `ADDR11 x -> hex_string_of_vect x
11  | `ADDR16 x -> hex_string_of_vect x
12  | `DATA x -> "#0" ^ hex_string_of_vect x ^ "h"
13  | `DATA16 x -> "#0" ^ hex_string_of_vect x ^ "h"
14  | `BIT x -> "bit " ^ hex_string_of_vect (x: byte)
15  | `NBIT x -> "nbit " ^ hex_string_of_vect (x: byte)
16  | `REG (r1, r2, r3) -> "R" ^ string_of_int (int_of_vect (mk_nibble false r1 r2 r3))
17  | `REL x -> hex_string_of_vect x
18  | `A_DPTR -> "@DPTR"
19  | `A_PC -> "@PC"
20  | `DIRECT x -> "0" ^ (hex_string_of_vect (x: byte)) ^ "h"
21  | `EXT_INDIRECT x -> "ext_indirect " ^ string_of_bool x
22  | `EXT_IND_DPTR -> "@DPTR"
23(* DPM: weird: this seems to be reversed in mcu8051ide: change made. *)
24  | `INDIRECT x -> if not x then "@R0" else "@R1"
25  | `IND_DPTR -> "@DPTR"
26  | `Label s -> s
27;;
28
29let pp_jump =
30 function
31    `CJNE (`U1 (a1,a2),a3) -> "cjne " ^ pp_arg a1 ^ ", " ^ pp_arg a2 ^ ", " ^ pp_arg a3
32  | `CJNE (`U2 (a1,a2),a3) -> "cjne " ^ pp_arg a1 ^ ", " ^ pp_arg a2 ^ ", " ^ pp_arg a3
33  | `DJNZ (a1,a2) -> "djnz " ^ pp_arg a1 ^ ", " ^ pp_arg a2
34  | `JB (a1,a2) -> "jb " ^ pp_arg a1 ^ ", " ^ pp_arg a2
35  | `JBC (a1,a2) -> "jbc " ^ pp_arg a1 ^ ", " ^ pp_arg a2
36  | `JC a1 -> "jc " ^ pp_arg a1
37  | `JNB (a1,a2) -> "jnb " ^ pp_arg a1 ^ ", " ^ pp_arg a2
38  | `JNC a1 -> "jnc " ^ pp_arg a1
39  | `JNZ a1 -> "jnz " ^ pp_arg a1
40  | `JZ a1 -> "jz  " ^ pp_arg a1
41
42let pp_instruction =
43 function
44    `Label l -> l ^ ":"
45  | `Cost l -> l ^ ":"
46  | `Jmp j -> "Jmp " ^ j
47  | `Call j -> "Call " ^ j
48  | `WithLabel i -> pp_jump i
49  | `Begin_fun -> "\n; Begin function"
50  | `End_fun -> "; End function\n"
51  |  (#jump as i) -> pp_jump i
52  | `Mov (a1,a2) -> "mov " ^ pp_arg a1 ^ ", " ^ a2
53  | `ACALL a1 -> "acall " ^ pp_arg a1
54  | `ADD (a1,a2) -> "add " ^ pp_arg a1 ^ ", " ^ pp_arg a2
55  | `ADDC (a1,a2) -> "addc " ^ pp_arg a1 ^ ", " ^ pp_arg a2
56  | `AJMP a1 -> "ajmp " ^ pp_arg a1
57  | `ANL (`U1 (a1,a2)) -> "anl " ^ pp_arg a1 ^ ", " ^ pp_arg a2
58  | `ANL (`U2 (a1,a2)) -> "anl " ^ pp_arg a1 ^ ", " ^ pp_arg a2
59  | `ANL (`U3 (a1,a2)) -> "anl " ^ pp_arg a1 ^ ", " ^ pp_arg a2
60  | `CLR a1 -> "clr " ^ pp_arg a1
61  | `CPL a1 -> "cpl " ^ pp_arg a1
62  | `DA a1 -> "da " ^ pp_arg a1
63  | `DEC a1 -> "dec " ^ pp_arg a1
64  | `DIV (a1,a2) -> "div AB"
65  | `INC a1 -> "inc " ^ pp_arg a1
66  | `JMP a1 -> "jmp " ^ pp_arg a1
67  | `LCALL a1 -> "lcall " ^ pp_arg a1
68  | `LJMP a1 -> "ljmp " ^ pp_arg a1
69  | `MOV (`U1 (a1,a2)) -> "mov " ^ pp_arg a1 ^ ", " ^ pp_arg a2
70  | `MOV (`U2 (a1,a2)) -> "mov " ^ pp_arg a1 ^ ", " ^ pp_arg a2
71  | `MOV (`U3 (a1,a2)) -> "mov " ^ pp_arg a1 ^ ", " ^ pp_arg a2
72  | `MOV (`U4 (a1,a2)) -> "mov " ^ pp_arg a1 ^ ", " ^ pp_arg a2
73  | `MOV (`U5 (a1,a2)) -> "mov " ^ pp_arg a1 ^ ", " ^ pp_arg a2
74  | `MOV (`U6 (a1,a2)) -> "mov " ^ pp_arg a1 ^ ", " ^ pp_arg a2
75  | `MOVC (a1,a2) -> "movc " ^ pp_arg a1 ^ ", " ^ pp_arg a2
76  | `MOVX (`U1 (a1,a2)) -> "movx " ^ pp_arg a1 ^ ", " ^ pp_arg a2
77  | `MOVX (`U2 (a1,a2)) -> "movx " ^ pp_arg a1 ^ ", " ^ pp_arg a2
78  | `MUL(a1, a2) -> "mul AB"
79  | `NOP -> "nop"
80  | `ORL (`U1(a1,a2)) -> "orl " ^ pp_arg a1 ^ ", " ^ pp_arg a2
81  | `ORL (`U2(a1,a2)) -> "orl " ^ pp_arg a1 ^ ", " ^ pp_arg a2
82  | `ORL (`U3(a1,a2)) -> "orl " ^ pp_arg a1 ^ ", " ^ pp_arg a2
83  | `POP a1 -> "pop " ^ pp_arg a1
84  | `PUSH a1 -> "push " ^ pp_arg a1
85  | `RET -> "ret"
86  | `RETI -> "reti"
87  | `RL a1 -> "rl " ^ pp_arg a1
88  | `RLC a1 -> "rlc " ^ pp_arg a1
89  | `RR a1 -> "rr " ^ pp_arg a1
90  | `RRC a1 -> "rrc " ^ pp_arg a1
91  | `SETB a1 -> "setb " ^ pp_arg a1
92  | `SJMP a1 -> "sjmp " ^ pp_arg a1
93  | `SUBB (a1,a2) -> "subb " ^ pp_arg a1 ^ ", " ^ pp_arg a2
94  | `SWAP a1 -> "swap " ^ pp_arg a1
95  | `XCH (a1,a2) -> "xch " ^ pp_arg a1 ^ ", " ^ pp_arg a2
96  | `XCHD(a1,a2) -> "xchd " ^ pp_arg a1 ^ ", " ^ pp_arg a2
97  | `XRL(`U1(a1,a2)) -> "xrl " ^ pp_arg a1 ^ ", " ^ pp_arg a2
98  | `XRL(`U2(a1,a2)) -> "xrl " ^ pp_arg a1 ^ ", " ^ pp_arg a2
99
100let print_program p =
101  let f s i = Printf.sprintf "%s%s\n" s (pp_instruction i) in
102  "Org 0\n\n" ^ (List.fold_left f "" p.ASM.code) ^ "\nEND\n"
Note: See TracBrowser for help on using the repository browser.