Changeset 200


Ignore:
Timestamp:
Oct 20, 2010, 1:35:59 PM (9 years ago)
Author:
mulligan
Message:

New features:

1) conditional jumps to labels implemented

[Note: all conditional jumps are to relative addresses;

if the label is too far away, an assert false is currently raised]

2) we now have a preamble of declarations of data labels in external

memory and a new pseudo-instruction "Mov" to place the value of a
data label into DPTR.

Location:
Deliverables/D4.1
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • Deliverables/D4.1/ASM.mli

    r158 r200  
    2525type addr16 = [ `ADDR16 of word ]
    2626
    27 type instruction =
     27type 'addr jump =
     28 [ `JC of 'addr
     29 | `JNC of 'addr
     30 | `JB of bit * 'addr
     31 | `JNB of bit * 'addr
     32 | `JBC of bit * 'addr
     33 | `JZ of 'addr
     34 | `JNZ of 'addr
     35 | `CJNE of (acc * [ direct | data ], [ reg | indirect ] * data) union2 * 'addr
     36 | `DJNZ of [ reg | direct ] * 'addr
     37 ]
     38
     39type 'addr preinstruction =
    2840 (* arithmetic operations *)
    2941 [ `ADD of acc * [ reg | direct | indirect | data ]
     
    7587
    7688 (* program branching *)
    77  | `JC of rel
    78  | `JNC of rel
    79  | `JB of bit * rel
    80  | `JNB of bit * rel
    81  | `JBC of bit * rel
     89 | 'addr jump
    8290 | `ACALL of addr11
    8391 | `LCALL of addr16
     
    8896 | `SJMP of rel
    8997 | `JMP of indirect_dptr
    90  | `JZ of rel
    91  | `JNZ of rel
    92  | `CJNE of (acc * [ direct | data ], [ reg | indirect ] * data) union2 * rel
    93  | `DJNZ of [ reg | direct ] * rel
    9498 | `NOP ]
     99
     100type instruction = rel preinstruction
    95101
    96102type labelled_instruction =
     
    100106 | `Jmp of string
    101107 | `Call of string
     108 | `Mov of dptr * string
     109 | `WithLabel of [`Label of string] jump
    102110 ]
     111
     112(* pairs <datalabel,size> *)
     113type preamble = (string * int) list
     114
     115type assembly_program = preamble * labelled_instruction list;;
  • Deliverables/D4.1/ASMInterpret.ml

    r199 r200  
    881881module IntMap = Map.Make(struct type t = int let compare = compare end);;
    882882
    883 let assembly l =
     883
     884let assembly_jump addr_of =
     885 function
     886    `JC a1 -> `JC (addr_of a1)
     887  | `JNC a1 -> `JNC (addr_of a1)
     888  | `JB (a1,a2) -> `JB (a1,addr_of a2)
     889  | `JNB (a1,a2) -> `JNB (a1,addr_of a2)
     890  | `JBC (a1,a2) -> `JBC (a1,addr_of a2)
     891  | `JZ a1 -> `JZ (addr_of a1)
     892  | `JNZ a1 -> `JNZ (addr_of a1)
     893  | `CJNE (a1,a2) -> `CJNE (a1,addr_of a2)
     894  | `DJNZ (a1,a2) -> `DJNZ (a1,addr_of a2)
     895;;
     896
     897let assembly (preamble,l) =
     898 let datalabels,_ =
     899  List.fold_left
     900   (fun (datalabels,addr) (name,size) ->
     901     let addr16 = vect_of_int addr `Sixteen in
     902      StringMap.add name addr16 datalabels, addr+size
     903   ) (StringMap.empty,0) preamble
     904 in
    884905 let pc,labels,costs =
    885906  List.fold_left
     
    888909        `Label s -> pc, StringMap.add s pc labels, costs
    889910      | `Cost s -> pc, labels, IntMap.add pc s costs
     911      | `Mov (_,_) -> pc, labels, costs
    890912      | `Jmp _
    891913      | `Call _ -> pc + 3, labels, costs  (*CSC: very stupid: always expand to worst opcode *)
     914      | `WithLabel i ->
     915          let fake_addr _ = `REL (zero `Eight) in
     916          let fake_jump = assembly_jump fake_addr i in
     917          let i',pc',_ = fetch (load_code_memory (assembly1 fake_jump)) (vect_of_int 0 `Sixteen) in
     918           assert (fake_jump = i');
     919           (pc + int_of_vect pc',labels, costs)
    892920      | #instruction as i ->
    893921        let i',pc',_ = fetch (load_code_memory (assembly1 i)) (vect_of_int 0 `Sixteen) in
     
    903931            `Label _
    904932          | `Cost _ -> []
     933          | `WithLabel i ->
     934              let addr_of (`Label s) =
     935               let addr = StringMap.find s labels in
     936               (* NOT IMPLEMENTED YET; NEEDS SMART ALGORITHM *)
     937                assert (addr < 256);
     938                `REL (vect_of_int addr `Eight)
     939              in
     940               assembly1 (assembly_jump addr_of i)
     941          | `Mov (`DPTR,s) ->
     942              let addrr16 = StringMap.find s datalabels in
     943               assembly1 (`MOV (`U4 (`DPTR,`DATA16 addrr16)))
    905944          | `Jmp s ->
    906945              let pc_offset = StringMap.find s labels in
  • Deliverables/D4.1/ASMInterpret.mli

    r195 r200  
    6767
    6868val assembly:
    69  ASM.labelled_instruction list -> BitVectors.byte list (*ASM.instruction list * symbol_table *) * string IntMap.t
     69 ASM.assembly_program -> BitVectors.byte list (*ASM.instruction list * symbol_table *) * string IntMap.t
    7070
    7171(*
  • Deliverables/D4.1/Pretty.ml

    r150 r200  
    2424  | `INDIRECT x -> if not x then "@R0" else "@R1"
    2525  | `IND_DPTR -> "@DPTR"
     26  | `Label s -> s
    2627;;
     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
    2741
    2842let pp_instruction =
     
    3044    `Label l -> l ^ ":"
    3145  | `Cost l -> l ^ ":"
    32   | `Jmp j -> "Jump: " ^ j
    33   | `Call j -> "Call: " ^ j
     46  | `Jmp j -> "Jump " ^ j
     47  | `Call j -> "Call " ^ j
     48  | `WithLabel i -> pp_jump i
     49  |  (#jump as i) -> pp_jump i
     50  | `Mov (a1,a2) -> "Mov " ^ pp_arg a1 ^ " " ^ a2
    3451  | `ACALL a1 -> "acall " ^ pp_arg a1
    3552  | `ADD (a1,a2) -> "add " ^ pp_arg a1 ^ " " ^ pp_arg a2
     
    3956  | `ANL (`U2 (a1,a2)) -> "anl " ^ pp_arg a1 ^ " " ^ pp_arg a2
    4057  | `ANL (`U3 (a1,a2)) -> "anl " ^ pp_arg a1 ^ " " ^ pp_arg a2
    41   | `CJNE (`U1 (a1,a2),a3) -> "cjne " ^ pp_arg a1 ^ " " ^ pp_arg a2 ^ " " ^ pp_arg a3
    42   | `CJNE (`U2 (a1,a2),a3) -> "cjne " ^ pp_arg a1 ^ " " ^ pp_arg a2 ^ " " ^ pp_arg a3
    4358  | `CLR a1 -> "clr " ^ pp_arg a1
    4459  | `CPL a1 -> "cpl " ^ pp_arg a1
     
    4661  | `DEC a1 -> "dec " ^ pp_arg a1
    4762  | `DIV (a1,a2) -> "div " ^ pp_arg a1 ^ " " ^ pp_arg a2
    48   | `DJNZ (a1,a2) -> "djnz " ^ pp_arg a1 ^ " " ^ pp_arg a2
    4963  | `INC a1 -> "inc " ^ pp_arg a1
    50   | `JB (a1,a2) -> "jb " ^ pp_arg a1 ^ " " ^ pp_arg a2
    51   | `JBC (a1,a2) -> "jbc " ^ pp_arg a1 ^ " " ^ pp_arg a2
    52   | `JC a1 -> "jc " ^ pp_arg a1
    5364  | `JMP a1 -> "jmp " ^ pp_arg a1
    54   | `JNB (a1,a2) -> "jnb " ^ pp_arg a1 ^ " " ^ pp_arg a2
    55   | `JNC a1 -> "jnc " ^ pp_arg a1
    56   | `JNZ a1 -> "jnz " ^ pp_arg a1
    57   | `JZ a1 -> "jz  " ^ pp_arg a1
    5865  | `LCALL a1 -> "lcall " ^ pp_arg a1
    5966  | `LJMP a1 -> "ljmp " ^ pp_arg a1
Note: See TracChangeset for help on using the changeset viewer.