Changeset 13 for branches/DelphiToC/UAssemblerSource.pas
- Timestamp:
- Apr 9, 2009, 9:53:40 AM (16 years ago)
- Location:
- branches/DelphiToC
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DelphiToC
-
Property svn:ignore
set to
*.dsk
*.exe
*.res
*.~dsk
*.dcu
-
Property svn:ignore
set to
-
branches/DelphiToC/UAssemblerSource.pas
r12 r13 2 2 3 3 interface 4 5 uses 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, StdCtrls, UPascalSource, UCodeProducer; 4 8 5 9 type … … 11 15 end; 12 16 17 TAssemblerLine = class 18 LabelName: string; 19 Instruction: string; 20 Operand1: string; 21 Operand2: string; 22 SourceCode: string; 23 function AsString: string; 24 end; 25 26 TAssemblerProducer = class(TCodeProducer) 27 AssemblyCode: TList; // TList<TAssemblerLine> 28 destructor Destroy; override; 29 private 30 procedure AddInstruction(LabelName, Instruction, Operand1, 31 Operand2: string); 32 procedure GenerateCommonBlock(CommonBlock: TCommonBlock; LabelPrefix: string); 33 procedure GenerateExpression(Expression: TExpression; LabelPrefix: string); 34 procedure GenerateProgram(ProgramBlock: TProgram); 35 end; 36 37 13 38 implementation 39 40 41 { TAssemblerLine } 42 43 function TAssemblerLine.AsString: string; 44 begin 45 if LabelName = '' then LabelName := #9 else 46 LabelName := LabelName + ':'#9; 47 if Operand2 <> '' then Operand1 := Operand1 + ', '; 48 49 Result := LabelName + Instruction + ' ' + Operand1 + Operand2; 50 end; 51 52 { TAssemblerProducer } 53 54 procedure TAssemblerProducer.AddInstruction(LabelName, Instruction, Operand1, 55 Operand2: string); 56 var 57 NewLine: TAssemblerLine; 58 begin 59 NewLine := TAssemblerLine.Create; 60 AssemblyCode.Add(NewLine); 61 NewLine.LabelName := LabelName; 62 NewLine.Instruction := Instruction; 63 NewLine.Operand1 := Operand1; 64 NewLine.Operand2 := Operand2; 65 end; 66 14 67 15 68 (* … … 43 96 *) 44 97 98 destructor TAssemblerProducer.Destroy; 99 var 100 I: Integer; 101 begin 102 for I := 0 to AssemblyCode.Count - 1 do 103 TAssemblerLine(AssemblyCode[I]).Free; 104 AssemblyCode.Free; 105 inherited; 106 end; 107 108 procedure TAssemblerProducer.GenerateCommonBlock(CommonBlock: TCommonBlock; LabelPrefix: string); 109 var 110 I: Integer; 111 LabelName: string; 112 begin 113 with CommonBlock do 114 for I := 0 to Operations.Count - 1 do 115 with TOperation(Operations[I]) do begin 116 if Referenced then LabelName := Name + '_L' + IntToStr(I) 117 else LabelName := ''; 118 case Instruction of 119 inJump: begin 120 AddInstruction(LabelName, 'JMP', Name + '_L' + IntToStr(GotoAddress), ''); 121 end; 122 inConditionalJump: begin 123 GenerateExpression(ExpressionTree, LabelPrefix + '_L' + IntToStr(GotoAddress)); 124 AddInstruction(LabelName, 'BRCS', Name + '_L' + IntToStr(GotoAddress), ''); 125 end; 126 inExpressionEvaluation: begin 127 if LabelName <> '' then AddInstruction(LabelName, '', '', ''); 128 GenerateExpression(ExpressionTree, Name + '_L' + IntToStr(GotoAddress)); 129 end; 130 inReturn: 131 AddInstruction(LabelName, 'RET', '', ''); 132 end; 133 end; 134 end; 135 136 procedure TAssemblerProducer.GenerateExpression(Expression: TExpression; LabelPrefix: string); 137 var 138 I: Integer; 139 begin 140 with Expression do 141 case NodeType of 142 ntNone: ; 143 ntVariable: if Assigned(Variable) then AddInstruction('', 'GETVAR', Variable.Name, ''); 144 nTFunction: AddInstruction('', 'CALL', Method.Name, ''); 145 ntConstant: AddInstruction('', 'CONST', '', ''); 146 ntOperator: begin 147 for I := 0 to SubItems.Count - 1 do 148 GenerateExpression(TExpression(SubItems[I]), LabelPrefix); 149 if OperatorName = '+' then AddInstruction('', 'ADD', '', '') 150 else if OperatorName = '-' then AddInstruction('', 'SUB', '', '') 151 else if OperatorName = '*' then AddInstruction('', 'MUL', '', '') 152 else if OperatorName = '/' then AddInstruction('', 'DIV', '', '') 153 else if OperatorName = 'div' then AddInstruction('', 'DIV', '', '') 154 else if OperatorName = 'mod' then AddInstruction('', 'MOD', '', '') 155 else if OperatorName = 'xor' then AddInstruction('', 'XOR', '', '') 156 else if OperatorName = 'or' then AddInstruction('', 'OR', '', '') 157 else if OperatorName = 'and' then AddInstruction('', 'AND', '', '') 158 else if OperatorName = 'not' then AddInstruction('', 'NEG', '', '') 159 else if OperatorName = ':=' then AddInstruction('', 'ST', '', '') 160 else if OperatorName = '>' then AddInstruction('', 'CP', '', '') 161 else if OperatorName = '>=' then AddInstruction('', 'CP', '', '') 162 else if OperatorName = '<' then AddInstruction('', 'CP', '', '') 163 else if OperatorName = '<=' then AddInstruction('', 'CP', '', '') 164 else if OperatorName = '=' then AddInstruction('', 'TST', '', '') 165 else if OperatorName = '<>' then AddInstruction('', 'CP', '', ''); 166 end; 167 end; 168 end; 169 170 procedure TAssemblerProducer.GenerateProgram(ProgramBlock: TProgram); 171 var 172 I: Integer; 173 begin 174 with ProgramBlock do 175 for I := 0 to Modules.Count - 1 do 176 GenerateCommonBlock(TModule(Modules[I]), ''); 177 end; 178 179 180 181 45 182 end.
Note:
See TracChangeset
for help on using the changeset viewer.