1
This commit is contained in:
parent
1bbc80de29
commit
858db4dc0e
4 changed files with 109 additions and 29 deletions
17
ass3/zbirnik/lib/drugiPrehod.ml
Normal file
17
ass3/zbirnik/lib/drugiPrehod.ml
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
(*funkcija drugega prehoda*)
|
||||||
|
open Simtab
|
||||||
|
open SemanticAnalyzer
|
||||||
|
|
||||||
|
let drugiPrehod (ifl : lineSemantic list) (simtab : symtab) =
|
||||||
|
let prviUkaz, ostaliUkazi =
|
||||||
|
match ifl with
|
||||||
|
| [] -> failwith "empty code"
|
||||||
|
| x :: xs -> x, xs
|
||||||
|
in
|
||||||
|
(*if Opcode == start then*)
|
||||||
|
let _ =
|
||||||
|
if prviUkaz.opcode = START then
|
||||||
|
(*TODO write listing line*)
|
||||||
|
in
|
||||||
|
(*TODO write header record to object program*)
|
||||||
|
(*TODO initialize first text record*)
|
||||||
|
|
@ -1,10 +1,16 @@
|
||||||
(*tukaj je vse povezano s prvim prehodom zbirnika*)
|
(*tukaj je vse povezano s prvim prehodom zbirnika*)
|
||||||
open SemanticAnalyzer
|
open SemanticAnalyzer
|
||||||
|
open Simtab
|
||||||
|
|
||||||
let startAddr = ref 0
|
|
||||||
let locctr = ref 0
|
|
||||||
|
|
||||||
let prviPrehod (code : lineSemantic list) =
|
|
||||||
|
(*funkcija prvega prehoda, !!! za END ne sme biti nič več definiranih ukazov ali direktiv!*)
|
||||||
|
let prviPrehod (code : lineSemantic list) : int*lineSemantic list*symtab=
|
||||||
|
(*inicializiramo startAddr, loctr in simtab*)
|
||||||
|
let startAddr = ref 0 in
|
||||||
|
let locctr = ref 0 in
|
||||||
|
let simtab = create_symtab () in
|
||||||
|
|
||||||
let prviUkaz, ostaliUkazi =
|
let prviUkaz, ostaliUkazi =
|
||||||
match code with
|
match code with
|
||||||
| [] -> failwith "empty code"
|
| [] -> failwith "empty code"
|
||||||
|
|
@ -12,34 +18,47 @@ let prviPrehod (code : lineSemantic list) =
|
||||||
in
|
in
|
||||||
|
|
||||||
(*if Opcode == start then*)
|
(*if Opcode == start then*)
|
||||||
|
let intermediateFileList =
|
||||||
if prviUkaz.opcode = START then
|
if prviUkaz.opcode = START then
|
||||||
let stAdr = get_string_from_mnemType prviUkaz.mnem in
|
let stAdr = get_string_from_mnemType prviUkaz.mnem in
|
||||||
let stAdr = match stAdr with | Some s -> s | None -> "0" in
|
let stAdr = match stAdr with | Some s -> s | None -> "0" in
|
||||||
startAddr := (int_of_string stAdr);
|
startAddr := (int_of_string stAdr);
|
||||||
locctr := !startAddr;
|
locctr := !startAddr;
|
||||||
(*TODO write line to intermediate file*)
|
(*write line to intermediate file*)
|
||||||
else
|
prviUkaz.loc <- Some !locctr;
|
||||||
(*initialize locctr to 0*)
|
[prviUkaz]
|
||||||
locctr := 0
|
else
|
||||||
|
(*initialize locctr to 0*)
|
||||||
;
|
(locctr := 0;
|
||||||
|
[])
|
||||||
|
in
|
||||||
(*while opcode != END loop*)
|
(*while opcode != END loop*)
|
||||||
let rec loop ostaliUkazi =
|
let rec loop ostaliUkazi prgLen intermediateFileList =
|
||||||
match ostaliUkazi with
|
match ostaliUkazi with
|
||||||
| [] -> () (*smo konec*)
|
| [] -> prgLen, (List.rev intermediateFileList), simtab (*smo konec*)
|
||||||
| x :: _ when x.opcode = END -> () (*konec while loopa*) (*TODO write last line to intermediate file, save the locctr - st addr as prg len*)
|
| x :: _ when x.opcode = END -> (*konec while loopa*) (*write last line to intermediate file, save the locctr - st addr as prg len*)
|
||||||
| x :: xs -> match x.opcode with
|
x.loc <- Some !locctr;
|
||||||
| COMMENT -> loop xs (*if this is a comment line*)
|
let intermediateFileList = x :: intermediateFileList in
|
||||||
|
let prgLen = !locctr - !startAddr in
|
||||||
|
prgLen, (List.rev intermediateFileList), simtab (*vrnemo program length*)
|
||||||
|
| x :: xs -> match x.opcode with (*loopamo*)
|
||||||
|
| COMMENT -> loop xs prgLen intermediateFileList(*if this is a comment line*)
|
||||||
| _ ->
|
| _ ->
|
||||||
(*if there is a symmbol in label field*)
|
(*if there is a symmbol in label field*)
|
||||||
let s = match x.label with
|
begin
|
||||||
|
match x.label with
|
||||||
| None -> ()
|
| None -> ()
|
||||||
| Some s -> () (*search simtab for s ...*)
|
| Some s ->
|
||||||
in
|
(*search simtab for s ...*)
|
||||||
|
(*doda simbol ce še ne obstaja, sier faila*)
|
||||||
|
add_symbol simtab s !locctr
|
||||||
|
end;
|
||||||
|
|
||||||
|
(*write line to intermediate file*)
|
||||||
|
x.loc <- Some !locctr;
|
||||||
(*search opcode -> smo ze naredili v semanticni analizi, tu bomo le ustrezno povečali locctr*)
|
(*search opcode -> smo ze naredili v semanticni analizi, tu bomo le ustrezno povečali locctr*)
|
||||||
locctr := !locctr + x.len;
|
locctr := !locctr + x.len;
|
||||||
(*TODO write line to intermediate file*)
|
let intermediateFileList = x :: intermediateFileList in
|
||||||
loop xs (*read nex input line end while*)
|
loop xs prgLen (intermediateFileList) (*read nex input line end while*)
|
||||||
in loop ostaliUkazi
|
in loop ostaliUkazi 0 intermediateFileList
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -277,13 +277,45 @@ let getMnemType (opcode : mnemonic) (ext : bool) (operand : string list) : mnemo
|
||||||
| _, _ -> failwith (Printf.sprintf "Invalid operands for opcode %s: %d operands"
|
| _, _ -> failwith (Printf.sprintf "Invalid operands for opcode %s: %d operands"
|
||||||
(string_of_mnemonic opcode) (List.length operand))
|
(string_of_mnemonic opcode) (List.length operand))
|
||||||
|
|
||||||
let getLen (mnem : mnemonic) (ext : bool) (mnemType : mnemonic_type) : int =
|
let getLen (mnemType : mnemonic_type) (mnem : mnemonic) : int =
|
||||||
42 (*TODO*)
|
match mnemType with
|
||||||
|
(* ----------- directives with no memory ----------- *)
|
||||||
|
| MnemonicD
|
||||||
|
| MnemonicDn _
|
||||||
|
| COMMENT -> 0
|
||||||
|
|
||||||
|
(* ----------- instruction formats ----------- *)
|
||||||
|
| MnemonicF1 -> 1
|
||||||
|
| MnemonicF2n _
|
||||||
|
| MnemonicF2r _
|
||||||
|
| MnemonicF2rn _
|
||||||
|
| MnemonicF2rr _ -> 2
|
||||||
|
|
||||||
|
| MnemonicF3
|
||||||
|
| MnemonicF3m _ -> 3
|
||||||
|
|
||||||
|
| MnemonicF4m _ -> 4
|
||||||
|
|
||||||
|
(* ----------- storage directives ----------- *)
|
||||||
|
| MnemonicSn n ->
|
||||||
|
begin
|
||||||
|
match mnem with
|
||||||
|
| RESB -> n
|
||||||
|
| RESW -> 3*n
|
||||||
|
| _ -> failwith "Narobe mnemonic glede na mnemType" (* RESB *)
|
||||||
|
end
|
||||||
|
| MnemonicSd _ ->
|
||||||
|
begin
|
||||||
|
match mnem with
|
||||||
|
| BYTE -> 1
|
||||||
|
| WORD -> 3
|
||||||
|
| _ -> failwith "Narobe mnemoic glede na mnemType"
|
||||||
|
end
|
||||||
|
|
||||||
let checkLineSemantic (line : line) : lineSemantic =
|
let checkLineSemantic (line : line) : lineSemantic =
|
||||||
let mnem, ext = getMnem line.opcode in
|
let mnem, ext = getMnem line.opcode in
|
||||||
let mnemType = getMnemType mnem ext line.operand in
|
let mnemType = getMnemType mnem ext line.operand in
|
||||||
let dolzina = getLen mnem ext mnemType in
|
let dolzina = getLen mnemType mnem in
|
||||||
{line_no = line.line_no; label = line.label; ext = ext; opcode = mnem; mnem = mnemType; comment = line.comment; len = dolzina; loc = line.loc}
|
{line_no = line.line_no; label = line.label; ext = ext; opcode = mnem; mnem = mnemType; comment = line.comment; len = dolzina; loc = line.loc}
|
||||||
|
|
||||||
let checkLineSemanticOfCode (code : line list) : lineSemantic list =
|
let checkLineSemanticOfCode (code : line list) : lineSemantic list =
|
||||||
|
|
|
||||||
12
ass3/zbirnik/lib/simtab.ml
Normal file
12
ass3/zbirnik/lib/simtab.ml
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
(*simbolna tabela*)
|
||||||
|
|
||||||
|
type symtab = (string, int) Hashtbl.t
|
||||||
|
|
||||||
|
let create_symtab () : symtab =
|
||||||
|
Hashtbl.create 64
|
||||||
|
|
||||||
|
let add_symbol (tab : symtab) (label : string) (addr : int) =
|
||||||
|
if Hashtbl.mem tab label then
|
||||||
|
failwith "Duplicate symbol in simtab"
|
||||||
|
else
|
||||||
|
Hashtbl.add tab label addr
|
||||||
Loading…
Add table
Add a link
Reference in a new issue