Compilerbau: Assemblieren |
|
1module PPL.Instructions where
2
3type Code = [Instr]
4
5data Instr
6 = LoadI Int -- load int const
7 | LoadF Double -- load float const
8 | LoadS String -- load string const
9 | LoadU -- load undefined value
10 | LoadEL -- load empty list
11 | Load Address -- read data memory
12 | Store Address -- write data memory
13 | Pop -- remove arg from stack
14 | Dup -- duplicate top of stack
15 | Compute Opcode -- evaluate something
16 | SysCall Subroutine -- system call (svc)
17 | PushJ Dest -- subroutine call
18 | PopJ -- computed jump (subroutine return)
19 | Entry Int -- allocate stack frame
20 | Exit -- delete stack frame
21 | Branch Bool Dest -- gotos and branches
22 | Jump Dest
23 | Label Label -- symbolic jump target
24 | IllegalInstr String -- not yet implemented
25 deriving (Eq, Show)
26
27data Address
28 = LocA Int -- local address
29 | AbsA Int -- global address
30 deriving (Eq, Show)
31
32data Dest
33 = Symb Label
34 | Disp Int
35 deriving (Eq, Show)
36
37data Opcode
38 = OPabort
39 | OPabove
40 | OPaddf
41 | OPaddi
42 | OPappendl
43 | OPbitmap
44 | OPblack
45 | OPblackAndWhite
46 | OPconcatHorizontal
47 | OPconcatVertical
48 | OPconcl
49 | OPconcs
50 | OPcut
51 | OPdecri
52 | OPdiff
53 | OPdivf
54 | OPdivi
55 | OPeqf
56 | OPeqi
57 | OPf2s
58 | OPflipDiagonal
59 | OPflipHorizontal
60 | OPflipVertical
61 | OPgamma
62 | OPgef
63 | OPgei
64 | OPgrey
65 | OPgtf
66 | OPgti
67 | OPheight
68 | OPi2f
69 | OPi2s
70 | OPincri
71 | OPindexl
72 | OPinverseDiff
73 | OPinverseMean
74 | OPinvert
75 | OPisemptyl
76 | OPlengthl
77 | OPmaxf
78 | OPmaxi
79 | OPmaxp
80 | OPmean
81 | OPmergeHorizontal
82 | OPmergeVertical
83 | OPminf
84 | OPmini
85 | OPminp
86 | OPmodi
87 | OPmulf
88 | OPmuli
89 | OPmulp
90 | OPpartitionHorizontal
91 | OPpartitionVertical
92 | OPpaste
93 | OPconsl
94 | OPreduceColor
95 | OPreplicate
96 | OPresize
97 | OProtate
98 | OPround
99 | OPscale
100 | OPshift
101 | OPshrink
102 | OPsideBySide
103 | OPsplitHorizontal
104 | OPsplitVertical
105 | OPsubf
106 | OPsubi
107 | OPtaill
108 | OPterminate
109 | OPtrunc
110 | OPwhite
111 | OPwidth
112 deriving (Eq, Show)
113
114type Arg = String
115type Comment = String
116type Label = String
117type Subroutine = String
118
119type DataSeg = Int
120
121type Executable = (Code, DataSeg)
|
1begin
2 var
3 i,j : int := 7, 3;
4
5 -- if statement
6
7 if i < j
8 then
9 i, j := j, i
10 elseif i = j
11 then
12 i, j := i - 1, j + 1
13 else
14 i := i+1
15 endif;
16
17 -- while loop
18
19 while i < j
20 do
21 i, j := i + 1, j - 1
22 endwhile;
23
24 -- repeat loop
25
26 repeat
27 i := i + 1
28 until i > j;
29
30 -- block
31
32 begin
33 var
34 k : int := i;
35 var
36 j : int := k;
37
38 j := i;
39 k := j
40
41 end
42
43end
|
1.text
2 loadi 7
3 loadi 3
4 store m[1]
5 store m[0]
6 load m[1]
7 load m[0]
8 gti
9 brfalse l0
10 load m[1]
11 load m[0]
12 store m[1]
13 store m[0]
14 jmp l1
15l0:
16 load m[0]
17 load m[1]
18 eqi
19 brfalse l2
20 load m[0]
21 loadi 1
22 subi
23 load m[1]
24 loadi 1
25 addi
26 store m[1]
27 store m[0]
28 jmp l3
29l2:
30 load m[0]
31 loadi 1
32 addi
33 store m[0]
34l3:
35l1:
36 jmp l4
37l5:
38 load m[0]
39 loadi 1
40 addi
41 load m[1]
42 loadi 1
43 subi
44 store m[1]
45 store m[0]
46l4:
47 load m[1]
48 load m[0]
49 gti
50 brtrue l5
51l6:
52 load m[0]
53 loadi 1
54 addi
55 store m[0]
56 load m[0]
57 load m[1]
58 gti
59 brfalse l6
60 load m[0]
61 store m[2]
62 load m[2]
63 store m[3]
64 load m[0]
65 store m[3]
66 load m[3]
67 store m[2]
68 undef
69 store m[2]
70 undef
71 store m[3]
72 undef
73 store m[0]
74 undef
75 store m[1]
76 terminate
77
78.data 4
|
.text
0: loadi 7
1: loadi 3
2: store m[1]
3: store m[0]
4: load m[1]
5: load m[0]
6: gti
7: brfalse 6 --> 13
8: load m[1]
9: load m[0]
10: store m[1]
11: store m[0]
12: jmp 18 --> 30
13: load m[0]
14: load m[1]
15: eqi
16: brfalse 10 --> 26
17: load m[0]
18: loadi 1
19: subi
20: load m[1]
21: loadi 1
22: addi
23: store m[1]
24: store m[0]
25: jmp 5 --> 30
26: load m[0]
27: loadi 1
28: addi
29: store m[0]
30: jmp 9 --> 39
31: load m[0]
32: loadi 1
33: addi
34: load m[1]
35: loadi 1
36: subi
37: store m[1]
38: store m[0]
39: load m[1]
40: load m[0]
41: gti
42: brtrue -11 --> 31
43: load m[0]
44: loadi 1
45: addi
46: store m[0]
47: load m[0]
48: load m[1]
49: gti
50: brfalse -7 --> 43
51: load m[0]
52: store m[2]
53: load m[2]
54: store m[3]
55: load m[0]
56: store m[3]
57: load m[3]
58: store m[2]
59: undef
60: store m[2]
61: undef
62: store m[3]
63: undef
64: store m[0]
65: undef
66: store m[1]
67: terminate
.data 4
|
1module PPL.Assemble where
2
3import PPL.Instructions
4
5type LabTab = [(Label, Int)]
6
7assemble :: Executable -> Executable
8assemble (is, ds)
9 = (assemble1 is, ds)
10
11assemble1 :: Code -> Code
12assemble1 is
13 = let
14 labTab = buildLabTab [] 0 is
15 is' = filter noLabelInstr is
16 in
17 zipWith (compDispl labTab) [0..] is'
18
19--
20
21buildLabTab :: LabTab -> Int -> Code -> LabTab
22
23buildLabTab lt _ []
24 = lt
25
26buildLabTab lt i ((Label l) : is)
27 = buildLabTab ((l,i) : lt) i is
28
29buildLabTab lt i (_ : is)
30 = buildLabTab lt (i+1) is
31
32-- lookup label table
33
34labVal :: Label -> LabTab -> Int
35labVal lab lt
36 = maybe 0 id (lookup lab lt)
37
38
39noLabelInstr :: Instr -> Bool
40
41noLabelInstr (Label _) = False
42noLabelInstr _ = True
43
44compDispl :: LabTab -> Int -> Instr -> Instr
45
46compDispl lt i (Branch cond (Symb l))
47 = Branch cond (Disp (labVal l lt - i))
48
49compDispl lt i (Jump (Symb l))
50 = Jump (Disp (labVal l lt - i))
51
52compDispl lt i (PushJ (Symb l))
53 = PushJ (Disp (labVal l lt -i))
54
55compDispl _ _ instr
56 = instr
|
Letzte Änderung: 14.02.2012 | © Prof. Dr. Uwe Schmidt |