Changeset 126
- Timestamp:
- Jan 14, 2022, 7:13:36 PM (3 years ago)
- Location:
- trunk
- Files:
-
- 17 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Forms/UFormMain.pas
r116 r126 306 306 Core.CurrentTarget := TTarget(Core.Targets[0]); 307 307 Core.Optimizations.AddSub := ReadBoolWithDefault('OptimizationAddSubEnabled', True); 308 Core.Optimizations.SetZero := ReadBoolWithDefault('OptimizationSetZeroEnabled', True); 308 309 Core.Optimizations.Merge := ReadBoolWithDefault('OptimizationMerge', True); 309 310 Core.Optimizations.CopyMultiply := ReadBoolWithDefault('OptimizationCopyMultiplyEnabled', True); … … 321 322 WriteString('TargetName', Core.CurrentTarget.Name); 322 323 WriteBool('OptimizationAddSubEnabled', Core.Optimizations.AddSub); 324 WriteBool('OptimizationSetZeroEnabled', Core.Optimizations.SetZero); 323 325 WriteBool('OptimizationMerge', Core.Optimizations.Merge); 324 326 WriteBool('OptimizationCopyMultiplyEnabled', Core.Optimizations.CopyMultiply); -
trunk/Forms/UFormOptions.lfm
r103 r126 11 11 OnCreate = FormCreate 12 12 OnShow = FormShow 13 LCLVersion = '2. 0.0.4'13 LCLVersion = '2.2.0.4' 14 14 object ButtonOk: TButton 15 15 Left = 631 … … 46 46 object TabSheetGeneral: TTabSheet 47 47 Caption = 'General' 48 ClientHeight = 27749 ClientWidth = 58548 ClientHeight = 331 49 ClientWidth = 701 50 50 ParentFont = False 51 51 object Panel1: TPanel 52 52 Left = 0 53 Height = 27753 Height = 331 54 54 Top = 0 55 Width = 58555 Width = 701 56 56 Align = alClient 57 57 BevelOuter = bvNone 58 ClientHeight = 27759 ClientWidth = 58558 ClientHeight = 331 59 ClientWidth = 701 60 60 ParentFont = False 61 61 TabOrder = 0 62 62 object Label3: TLabel 63 63 Left = 10 64 Height = 2 064 Height = 26 65 65 Top = 58 66 Width = 1 2766 Width = 164 67 67 Caption = 'Interface language:' 68 ParentColor = False69 68 ParentFont = False 70 69 end 71 70 object ComboBoxLanguage: TComboBox 72 71 Left = 307 73 Height = 3372 Height = 41 74 73 Top = 58 75 74 Width = 226 76 ItemHeight = 2575 ItemHeight = 0 77 76 ParentFont = False 78 77 Style = csDropDownList … … 83 82 Height = 32 84 83 Top = 102 85 Width = 1 14484 Width = 1260 86 85 Anchors = [akTop, akLeft, akRight] 87 86 AutoSize = False … … 94 93 Height = 32 95 94 Top = 150 96 Width = 1 13495 Width = 1250 97 96 Anchors = [akTop, akLeft, akRight] 98 97 AutoSize = False … … 104 103 object LabelDPI: TLabel 105 104 Left = 36 106 Height = 2 0107 Top = 192 108 Width = 26105 Height = 26 106 Top = 192 107 Width = 35 109 108 Caption = 'DPI:' 110 ParentColor = False111 109 ParentFont = False 112 110 end 113 111 object SpinEditDPIX: TSpinEdit 114 112 Left = 134 115 Height = 28113 Height = 42 116 114 Top = 190 117 115 Width = 96 … … 124 122 object SpinEditDPIY: TSpinEdit 125 123 Left = 259 126 Height = 28124 Height = 42 127 125 Top = 192 128 126 Width = 96 … … 135 133 object LabelX: TLabel 136 134 Left = 240 137 Height = 2 0138 Top = 192 139 Width = 7135 Height = 26 136 Top = 192 137 Width = 10 140 138 Caption = 'x' 141 ParentColor = False142 139 ParentFont = False 143 140 end 144 141 object LabelTheme: TLabel 145 142 Left = 10 146 Height = 2 0143 Height = 26 147 144 Top = 19 148 Width = 48145 Width = 63 149 146 Caption = 'Theme:' 150 ParentColor = False151 147 ParentFont = False 152 148 end 153 149 object ComboBoxTheme: TComboBox 154 150 Left = 307 155 Height = 33151 Height = 41 156 152 Top = 10 157 153 Width = 226 158 ItemHeight = 25154 ItemHeight = 0 159 155 ParentFont = False 160 156 Style = csDropDownList … … 165 161 object TabSheetBuild: TTabSheet 166 162 Caption = 'Build' 167 ClientHeight = 33 3168 ClientWidth = 70 3163 ClientHeight = 331 164 ClientWidth = 701 169 165 ParentFont = False 170 166 object Panel2: TPanel 171 167 Left = 0 172 Height = 33 3168 Height = 331 173 169 Top = 0 174 Width = 70 3170 Width = 701 175 171 Align = alClient 176 172 BevelOuter = bvNone 177 ClientHeight = 33 3178 ClientWidth = 70 3173 ClientHeight = 331 174 ClientWidth = 701 179 175 ParentFont = False 180 176 TabOrder = 0 181 177 object Label4: TLabel 182 178 Left = 10 183 Height = 2 5179 Height = 26 184 180 Top = 125 185 Width = 189181 Width = 202 186 182 Caption = 'Compiler optimizations:' 187 ParentColor = False188 183 ParentFont = False 189 184 end 190 185 object ComboBoxOptimization: TComboBox 191 186 Left = 298 192 Height = 3 3193 Top = 12 5187 Height = 38 188 Top = 120 194 189 Width = 226 195 ItemHeight = 25190 ItemHeight = 0 196 191 Items.Strings = ( 197 192 'None' … … 205 200 object CheckBoxOptimizeAddSub: TCheckBox 206 201 Left = 37 207 Height = 29202 Height = 30 208 203 Top = 163 209 Width = 2 28204 Width = 236 210 205 Caption = 'Addition and subtraction' 211 206 ParentFont = False … … 214 209 object CheckBoxOptimizeMerge: TCheckBox 215 210 Left = 37 216 Height = 29217 Top = 192218 Width = 2 18211 Height = 30 212 Top = 224 213 Width = 227 219 214 Caption = 'Merge same operations' 220 215 ParentFont = False … … 223 218 object CheckBoxOptimizeCopyMultiply: TCheckBox 224 219 Left = 37 225 Height = 29226 Top = 2 50227 Width = 14 1220 Height = 30 221 Top = 282 222 Width = 142 228 223 Caption = 'Copy multiply' 224 OnChange = CheckBoxOptimizeCopyMultiplyChange 229 225 ParentFont = False 230 226 TabOrder = 3 … … 232 228 object CheckBoxOptimizeRelativeIndexes: TCheckBox 233 229 Left = 37 234 Height = 29235 Top = 2 21236 Width = 1 55230 Height = 30 231 Top = 253 232 Width = 162 237 233 Caption = 'Relative indexes' 234 OnChange = CheckBoxOptimizeRelativeIndexesChange 238 235 ParentFont = False 239 236 TabOrder = 4 … … 241 238 object Label2: TLabel 242 239 Left = 10 243 Height = 2 5244 Top = 48245 Width = 66240 Height = 26 241 Top = 64 242 Width = 73 246 243 Caption = 'Cell size:' 247 ParentColor = False248 244 ParentFont = False 249 245 end 250 246 object Label1: TLabel 251 247 Left = 10 252 Height = 2 5253 Top = 1 0254 Width = 1 05248 Height = 26 249 Top = 16 250 Width = 112 255 251 Caption = 'Memory size:' 256 ParentColor = False257 252 ParentFont = False 258 253 end 259 254 object SpinEditMemorySize: TSpinEdit 260 255 Left = 198 261 Height = 33256 Height = 42 262 257 Top = 7 263 258 Width = 156 … … 268 263 object SpinEditCellSize: TSpinEdit 269 264 Left = 198 270 Height = 33271 Top = 48265 Height = 42 266 Top = 56 272 267 Width = 156 273 268 MaxValue = 2000000000 274 269 ParentFont = False 275 270 TabOrder = 6 271 end 272 object CheckBoxOptimizeSetZero: TCheckBox 273 Left = 37 274 Height = 30 275 Top = 192 276 Width = 93 277 Caption = 'Set zero' 278 ParentFont = False 279 TabOrder = 7 276 280 end 277 281 end -
trunk/Forms/UFormOptions.lrj
r103 r126 17 17 {"hash":150963587,"name":"tformoptions.checkboxoptimizerelativeindexes.caption","sourcebytes":[82,101,108,97,116,105,118,101,32,105,110,100,101,120,101,115],"value":"Relative indexes"}, 18 18 {"hash":243182762,"name":"tformoptions.label2.caption","sourcebytes":[67,101,108,108,32,115,105,122,101,58],"value":"Cell size:"}, 19 {"hash":239490586,"name":"tformoptions.label1.caption","sourcebytes":[77,101,109,111,114,121,32,115,105,122,101,58],"value":"Memory size:"} 19 {"hash":239490586,"name":"tformoptions.label1.caption","sourcebytes":[77,101,109,111,114,121,32,115,105,122,101,58],"value":"Memory size:"}, 20 {"hash":208144671,"name":"tformoptions.checkboxoptimizesetzero.caption","sourcebytes":[83,101,116,32,122,101,114,111],"value":"Set zero"} 20 21 ]} -
trunk/Forms/UFormOptions.pas
r115 r126 20 20 CheckBoxOptimizeAddSub: TCheckBox; 21 21 CheckBoxOptimizeCopyMultiply: TCheckBox; 22 CheckBoxOptimizeSetZero: TCheckBox; 22 23 CheckBoxOptimizeMerge: TCheckBox; 23 24 CheckBoxOptimizeRelativeIndexes: TCheckBox; … … 42 43 TabSheetBuild: TTabSheet; 43 44 procedure CheckBoxDPIAutoChange(Sender: TObject); 45 procedure CheckBoxOptimizeCopyMultiplyChange(Sender: TObject); 46 procedure CheckBoxOptimizeRelativeIndexesChange(Sender: TObject); 44 47 procedure FormClose(Sender: TObject; var CloseAction: TCloseAction); 45 48 procedure FormCreate(Sender: TObject); … … 81 84 end; 82 85 CheckBoxOptimizeAddSub.Enabled := ComboBoxOptimization.ItemIndex > 0; 86 CheckBoxOptimizeSetZero.Enabled := ComboBoxOptimization.ItemIndex > 0; 83 87 CheckBoxOptimizeMerge.Enabled := ComboBoxOptimization.ItemIndex > 0; 84 88 CheckBoxOptimizeRelativeIndexes.Enabled := ComboBoxOptimization.ItemIndex > 0; … … 94 98 CheckBox1.Checked := Core.OpenProjectOnStart; 95 99 CheckBoxOptimizeAddSub.Checked := Core.Optimizations.AddSub; 100 CheckBoxOptimizeSetZero.Checked := Core.Optimizations.SetZero; 96 101 CheckBoxOptimizeMerge.Checked := Core.Optimizations.Merge; 97 102 CheckBoxOptimizeRelativeIndexes.Checked := Core.Optimizations.RelativeIndexes; … … 119 124 Core.MemorySize := SpinEditMemorySize.Value; 120 125 Core.Optimizations.AddSub := CheckBoxOptimizeAddSub.Checked; 126 Core.Optimizations.SetZero := CheckBoxOptimizeSetZero.Checked; 121 127 Core.Optimizations.Merge := CheckBoxOptimizeMerge.Checked; 122 128 Core.Optimizations.RelativeIndexes := CheckBoxOptimizeRelativeIndexes.Checked; … … 127 133 begin 128 134 UpdateInterface; 135 end; 136 137 procedure TFormOptions.CheckBoxOptimizeCopyMultiplyChange(Sender: TObject); 138 begin 139 // Needed for optimize copy multiply 140 if CheckBoxOptimizeCopyMultiply.Checked then 141 CheckBoxOptimizeRelativeIndexes.Checked := True; 142 end; 143 144 procedure TFormOptions.CheckBoxOptimizeRelativeIndexesChange(Sender: TObject); 145 begin 146 // Needed for optimize copy multiply 147 if not CheckBoxOptimizeRelativeIndexes.Checked then 148 CheckBoxOptimizeCopyMultiply.Checked := False; 129 149 end; 130 150 -
trunk/Languages/LazFuck.cs.po
r124 r126 287 287 msgstr "Relativnà indexy" 288 288 289 #: tformoptions.checkboxoptimizesetzero.caption 290 msgid "Set zero" 291 msgstr "Nastavenà nuly" 292 289 293 #: tformoptions.label1.caption 290 294 msgid "Memory size:" … … 544 548 msgstr "SpouÅ¡tÄÄ \"%s\" nenalezen" 545 549 546 #: utargetinterpretter.sjumptablecol ision547 msgid "Jump table col ision"548 msgstr "Kolize skokové tabulky"550 #: utargetinterpretter.sjumptablecollision 551 msgid "Jump table collision" 552 msgstr "Kolize v tabulce skoků" 549 553 550 554 #: utargetinterpretter.sjumptableinsistent -
trunk/Languages/LazFuck.pot
r124 r126 277 277 msgstr "" 278 278 279 #: tformoptions.checkboxoptimizesetzero.caption 280 msgid "Set zero" 281 msgstr "" 282 279 283 #: tformoptions.label1.caption 280 284 msgid "Memory size:" … … 534 538 msgstr "" 535 539 536 #: utargetinterpretter.sjumptablecol ision537 msgid "Jump table col ision"540 #: utargetinterpretter.sjumptablecollision 541 msgid "Jump table collision" 538 542 msgstr "" 539 543 -
trunk/Target/UTargetC.pas
r125 r126 74 74 AddLine('Pos = 0;'); 75 75 FProgramIndex := 0; 76 while (FProgramIndex < Length(FProgram))do begin76 while FProgramIndex < FProgram.Count do begin 77 77 case FProgram[FProgramIndex].Command of 78 78 cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); … … 83 83 cmInput: AddLine(GetMemoryCell + ' = getchar();'); 84 84 cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 85 cmMultip y: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');85 cmMultiply: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 86 86 cmLoopStart: begin 87 87 AddLine('while(' + GetMemoryCell + ' != 0)'); -
trunk/Target/UTargetCSharp.pas
r125 r126 75 75 AddLine('Pos = 0;'); 76 76 FProgramIndex := 0; 77 while (FProgramIndex < Length(FProgram))do begin77 while FProgramIndex < FProgram.Count do begin 78 78 case FProgram[FProgramIndex].Command of 79 79 cmPointerInc: AddLine('Pos += ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); … … 84 84 cmInput: AddLine(GetMemoryCell + ' = (int)Console.ReadKey().KeyChar;'); 85 85 cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 86 cmMultip y: begin86 cmMultiply: begin 87 87 if FProgram[FProgramIndex].Parameter = 1 then 88 88 AddLine(GetMemoryCell + ' += Memory[Pos];') -
trunk/Target/UTargetDelphi.pas
r125 r126 67 67 AddLine('Pos := 0;'); 68 68 FProgramIndex := 0; 69 while (FProgramIndex < Length(FProgram))do begin69 while FProgramIndex < FProgram.Count do begin 70 70 case FProgram[FProgramIndex].Command of 71 71 cmPointerInc: AddLine('Inc(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); … … 74 74 cmDec: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 75 75 cmSet: AddLine(GetMemoryCell + ' := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 76 cmMultip y: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');76 cmMultiply: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 77 77 cmOutput: AddLine('Write(Chr(' + GetMemoryCell + '));'); 78 78 cmInput: AddLine('Read(ReadChar); ' + GetMemoryCell + ' := Ord(ReadChar);'); -
trunk/Target/UTargetFPC.pas
r125 r126 69 69 AddLine('Pos := 0;'); 70 70 FProgramIndex := 0; 71 while (FProgramIndex < Length(FProgram))do begin71 while FProgramIndex < FProgram.Count do begin 72 72 case FProgram[FProgramIndex].Command of 73 73 cmPointerInc: AddLine('Inc(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); … … 76 76 cmDec: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 77 77 cmSet: AddLine(GetMemoryCell + ' := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 78 cmMultip y: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');78 cmMultiply: AddLine(GetMemoryCell + ' := ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 79 79 cmOutput: AddLine('Write(Chr(' + GetMemoryCell + '));'); 80 80 cmInput: AddLine('Read(ReadChar); ' + GetMemoryCell + ' := Ord(ReadChar);'); -
trunk/Target/UTargetInterpretter.pas
r125 r126 83 83 SProgramUpperLimit = 'Program run over upper limit'; 84 84 SJumpTableInsistent = 'Jump table is inconsistent'; 85 SJumpTableCol ision = 'Jump table colision';85 SJumpTableCollision = 'Jump table collision'; 86 86 SProgramNotRunning = 'Program not running'; 87 87 SUnsupportedCommand = 'Unsupported command'; … … 95 95 with Parent do 96 96 repeat 97 while (FProgramIndex < Length(FProgram)) and (State <> rsStopped) do begin97 while (FProgramIndex < FProgram.Count) and (State <> rsStopped) do begin 98 98 if State = rsRunning then begin 99 99 if FProgramBreakpoints[FProgramIndex] then begin … … 154 154 I: Integer; 155 155 begin 156 for I := 0 to Length(FProgram)- 1 do begin156 for I := 0 to FProgram.Count - 1 do begin 157 157 case FProgram[I].Command of 158 cmLoopStart: FProgram [I].Parameter := 0;159 cmLoopEnd: FProgram [I].Parameter := 0;158 cmLoopStart: FProgram.Operations[I].Parameter := 0; 159 cmLoopEnd: FProgram.Operations[I].Parameter := 0; 160 160 end; 161 161 end; 162 162 163 163 SetLength(Loop, 0); 164 for I := 0 to Length(FProgram)- 1 do begin164 for I := 0 to FProgram.Count - 1 do begin 165 165 case FProgram[I].Command of 166 166 cmLoopStart: begin … … 170 170 cmLoopEnd: begin 171 171 if FProgram[I].Parameter > 0 then 172 raise Exception.Create(SJumpTableCol ision);173 FProgram [I].Parameter := Loop[High(Loop)];172 raise Exception.Create(SJumpTableCollision); 173 FProgram.Operations[I].Parameter := Loop[High(Loop)]; 174 174 if FProgram[Loop[High(Loop)]].Parameter > 0 then 175 raise Exception.Create(SJumpTableCol ision);176 FProgram [Loop[High(Loop)]].Parameter := I;175 raise Exception.Create(SJumpTableCollision); 176 FProgram.Operations[Loop[High(Loop)]].Parameter := I; 177 177 SetLength(Loop, Length(Loop) - 1); 178 178 end; … … 303 303 I: Integer; 304 304 begin 305 SetLength(FProgramBreakpoints, Length(FProgram));305 SetLength(FProgramBreakpoints, FProgram.Count); 306 306 for I := 0 to High(FProgramBreakpoints) do 307 307 FProgramBreakpoints[I] := False; 308 308 for I := 0 to BreakPoints.Count - 1 do 309 if TBreakPoint(BreakPoints[I]).TargetAddress < Length(FProgramBreakpoints) then310 FProgramBreakpoints[ TBreakPoint(BreakPoints[I]).TargetAddress] := True;309 if BreakPoints[I].TargetAddress < Length(FProgramBreakpoints) then 310 FProgramBreakpoints[BreakPoints[I].TargetAddress] := True; 311 311 end; 312 312 … … 316 316 begin 317 317 Result := ''; 318 for I := 0 to Length(FProgram)- 1 do begin318 for I := 0 to FProgram.Count - 1 do begin 319 319 Result := Result + GetOperationText(FProgram[I]); 320 320 end; … … 438 438 // Extended commands 439 439 FCommandTable[cmSet] := CommandSet; 440 FCommandTable[cmMultip y] := CommandMultiply;440 FCommandTable[cmMultiply] := CommandMultiply; 441 441 end; 442 442 -
trunk/Target/UTargetJava.pas
r125 r126 74 74 AddLine('Pos = 0;'); 75 75 FProgramIndex := 0; 76 while (FProgramIndex < Length(FProgram))do begin76 while FProgramIndex < FProgram.Count do begin 77 77 case FProgram[FProgramIndex].Command of 78 78 cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); … … 83 83 cmInput: AddLine(GetMemoryCell + ' = (char)System.in.read();'); 84 84 cmSet: AddLine(GetMemoryCell + ' = (char)' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 85 cmMultip y: AddLine(GetMemoryCell + ' = (char)((int)' + GetMemoryCell + ' + (int)Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');85 cmMultiply: AddLine(GetMemoryCell + ' = (char)((int)' + GetMemoryCell + ' + (int)Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 86 86 cmLoopStart: begin 87 87 AddLine('while(' + GetMemoryCell + ' != 0)'); -
trunk/Target/UTargetJavascript.pas
r125 r126 61 61 AddLine(''); 62 62 FProgramIndex := 0; 63 while (FProgramIndex < Length(FProgram))do begin63 while FProgramIndex < FProgram.Count do begin 64 64 case FProgram[FProgramIndex].Command of 65 65 cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); … … 70 70 cmInput: ; //AddLine(GetMemoryCell + ' = getchar();'); 71 71 cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 72 cmMultip y: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');72 cmMultiply: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + Memory[Pos] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 73 73 cmLoopStart: begin 74 74 AddLine('while(' + GetMemoryCell + ' != 0)'); -
trunk/Target/UTargetPHP.pas
r125 r126 66 66 AddLine('$Position = 0;'); 67 67 FProgramIndex := 0; 68 while (FProgramIndex < Length(FProgram))do begin68 while FProgramIndex < FProgram.Count do begin 69 69 case FProgram[FProgramIndex].Command of 70 70 cmPointerInc: AddLine('$Position = $Position + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); … … 77 77 cmInput: AddLine(GetMemoryCell + ' = fgetc(STDIN);'); 78 78 cmSet: AddLine(GetMemoryCell + ' = chr(' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 79 cmMultip y: AddLine(GetMemoryCell + ' = chr(ord(' + GetMemoryCell + ') + ord($Memory[$Position]) * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');');79 cmMultiply: AddLine(GetMemoryCell + ' = chr(ord(' + GetMemoryCell + ') + ord($Memory[$Position]) * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 80 80 cmLoopStart: begin 81 81 AddLine('while(' + GetMemoryCell + ' != "\0") {'); -
trunk/Target/UTargetPython.pas
r125 r126 104 104 AddLine('position = 0'); 105 105 FProgramIndex := 0; 106 while (FProgramIndex < Length(FProgram))do begin106 while FProgramIndex < FProgram.Count do begin 107 107 case FProgram[FProgramIndex].Command of 108 108 cmPointerInc: AddLine('position += ' + IntToStr(FProgram[FProgramIndex].Parameter)); … … 116 116 cmInput: AddLine(GetMemoryCell + ' = ord(getchar())'); 117 117 cmSet: AddLine(GetMemoryCell + ' = ' + IntToStr(FProgram[FProgramIndex].Parameter)); 118 cmMultip y: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + memory[position] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';');118 cmMultiply: AddLine(GetMemoryCell + ' = ' + GetMemoryCell + ' + memory[position] * ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 119 119 cmLoopStart: begin 120 120 AddLine('while(' + GetMemoryCell + ' != 0):'); -
trunk/UBFTarget.pas
r125 r126 11 11 12 12 TMachineCommand = (cmNoOperation, cmInc, cmDec, cmPointerInc, cmPointerDec, 13 cmOutput, cmInput, cmLoopStart, cmLoopEnd, cmDebug, cmSet, cmMultip y);13 cmOutput, cmInput, cmLoopStart, cmLoopEnd, cmDebug, cmSet, cmMultiply); 14 14 15 15 { TMachineOperation } … … 19 19 Parameter: Integer; 20 20 RelIndex: Integer; 21 class function Create(Command: TMachineCommand; Parameter, RelIndex: Integer): TMachineOperation; static; 21 class function Create(Command: TMachineCommand; Parameter: Integer; 22 RelIndex: Integer = 0): TMachineOperation; static; 23 end; 24 25 TMachineOperations = array of TMachineOperation; 26 27 { TProgram } 28 29 TProgram = class 30 private 31 function GetCount: Integer; 32 function GetItem(Index: Integer): TMachineOperation; 33 procedure SetCount(AValue: Integer); 34 procedure SetItem(Index: Integer; AValue: TMachineOperation); 35 public 36 Operations: TMachineOperations; 37 Index: Integer; 38 procedure Assign(Source: TProgram); 39 property Count: Integer read GetCount write SetCount; 40 property Items[Index: Integer]: TMachineOperation read GetItem write SetItem; default; 22 41 end; 23 42 24 43 TOptimizations = record 25 44 AddSub: Boolean; 45 SetZero: Boolean; 26 46 Merge: Boolean; 27 47 RelativeIndexes: Boolean; … … 33 53 TBFTarget = class(TTarget) 34 54 private 35 function Check Clear: Boolean;55 function CheckLoopSetZero: Boolean; 36 56 function CheckOccurenceSumParam(C: TMachineCommand): Integer; 37 57 function CheckOccurence(C: TMachineCommand): Integer; 58 function CheckLoopDecrementCount: Integer; 38 59 procedure OptimizeAddSub; 60 procedure OptimizeSetZero; 39 61 procedure OptimizeMerge; 40 62 procedure OptimizeZeroInitMemory; … … 42 64 procedure OptimizeCopyMultiply; 43 65 protected 44 FProgram: array of TMachineOperation;66 FProgram: TProgram; 45 67 FProgramIndex: Integer; 46 68 function GetOperationText(Operation: TMachineOperation): string; virtual; … … 52 74 Optimizations: TOptimizations; 53 75 constructor Create; override; 76 destructor Destroy; override; 54 77 procedure OptimizeSource; override; 55 78 property ProgramIndex: Integer read FProgramIndex; … … 66 89 SUnsupportedCommand = 'Unsupported command %d'; 67 90 91 { TProgram } 92 93 function TProgram.GetCount: Integer; 94 begin 95 Result := Length(Operations); 96 end; 97 98 function TProgram.GetItem(Index: Integer): TMachineOperation; 99 begin 100 Result := Operations[Index]; 101 end; 102 103 procedure TProgram.SetCount(AValue: Integer); 104 begin 105 SetLength(Operations, AValue); 106 end; 107 108 procedure TProgram.SetItem(Index: Integer; AValue: TMachineOperation); 109 begin 110 Operations[Index] := AValue; 111 end; 112 113 procedure TProgram.Assign(Source: TProgram); 114 begin 115 Count := Source.Count; 116 Move(Pointer(Source.Operations)^, Pointer(Operations)^, SizeOf(TMachineOperation) * Count); 117 end; 118 68 119 { TMachineOperation } 69 120 70 class function TMachineOperation.Create(Command: TMachineCommand; Parameter,71 RelIndex: Integer): TMachineOperation;121 class function TMachineOperation.Create(Command: TMachineCommand; 122 Parameter: Integer; RelIndex: Integer = 0): TMachineOperation; 72 123 begin 73 124 Result.Command := Command; … … 76 127 end; 77 128 78 function TBFTarget.Check Clear: Boolean;79 begin 80 Result := (FProgram[FProgramIndex].Command = cmLoopStart) and ( Length(FProgram)>= FProgramIndex + 2) and129 function TBFTarget.CheckLoopSetZero: Boolean; 130 begin 131 Result := (FProgram[FProgramIndex].Command = cmLoopStart) and (FProgram.Count >= FProgramIndex + 2) and 81 132 (((FProgram[FProgramIndex + 1].Command = cmDec) and (FProgram[FProgramIndex + 1].Parameter = 1)) or 82 133 ((FProgram[FProgramIndex + 1].Command = cmInc) and (FProgram[FProgramIndex + 1].Parameter = -1))) … … 87 138 begin 88 139 Result := 1; 89 while ((FProgramIndex + 1) < Length(FProgram)) and (FProgram[FProgramIndex + 1].Command = C) do begin140 while ((FProgramIndex + 1) < FProgram.Count) and (FProgram[FProgramIndex + 1].Command = C) do begin 90 141 Inc(Result); 91 142 Inc(FProgramIndex); … … 96 147 begin 97 148 Result := FProgram[FProgramIndex].Parameter; 98 while ((FProgramIndex + 1) < Length(FProgram)) and (FProgram[FProgramIndex + 1].Command = C) do begin149 while ((FProgramIndex + 1) < FProgram.Count) and (FProgram[FProgramIndex + 1].Command = C) do begin 99 150 Inc(Result, FProgram[FProgramIndex + 1].Parameter); 100 151 Inc(FProgramIndex); … … 102 153 end; 103 154 155 // Merge multiple sequential occurences of +/-/>/< operations into single fast 156 // operation with parameter value set to number of occurences 104 157 procedure TBFTarget.OptimizeAddSub; 105 158 var 106 NewProgram: array of TMachineOperation; 107 NewProgramIndex: Integer; 108 NewTargetPos: Integer; 159 NewProgram: TProgram; 160 NewTargetIndex: Integer; 109 161 FirstIndex: Integer; 110 162 begin 111 New ProgramIndex := 0;112 New TargetPos := 0;113 SetLength(NewProgram, Length(FProgram));163 NewTargetIndex := 0; 164 NewProgram := TProgram.Create; 165 NewProgram.Count := FProgram.Count; 114 166 115 167 FProgramIndex := 0; 116 while (FProgramIndex < Length(FProgram))do begin168 while FProgramIndex < FProgram.Count do begin 117 169 FirstIndex := FProgramIndex; 118 170 case FProgram[FProgramIndex].Command of 119 171 cmPointerInc: begin 120 NewProgram[NewProgram Index].Command := cmPointerInc;121 NewProgram[NewProgramIndex].Parameter := CheckOccurenceSumParam(cmPointerInc);172 NewProgram[NewProgram.Index] := TMachineOperation.Create(cmPointerInc, 173 CheckOccurenceSumParam(cmPointerInc)); 122 174 end; 123 175 cmPointerDec: begin 124 NewProgram[NewProgram Index].Command := cmPointerDec;125 NewProgram[NewProgramIndex].Parameter := CheckOccurenceSumParam(cmPointerDec);176 NewProgram[NewProgram.Index] := TMachineOperation.Create(cmPointerDec, 177 CheckOccurenceSumParam(cmPointerDec)); 126 178 end; 127 179 cmInc: begin 128 NewProgram[NewProgram Index].Command := cmInc;129 NewProgram[NewProgramIndex].Parameter := CheckOccurenceSumParam(cmInc);180 NewProgram[NewProgram.Index] := TMachineOperation.Create(cmInc, 181 CheckOccurenceSumParam(cmInc)); 130 182 end; 131 183 cmDec: begin 132 NewProgram[NewProgram Index].Command := cmDec;133 NewProgram[NewProgramIndex].Parameter := CheckOccurenceSumParam(cmDec);134 end; 135 else NewProgram[NewProgram Index] := FProgram[FProgramIndex];184 NewProgram[NewProgram.Index] := TMachineOperation.Create(cmDec, 185 CheckOccurenceSumParam(cmDec)); 186 end; 187 else NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 136 188 end; 137 DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgramIndex, NewTargetPos); 138 Inc(NewTargetPos, Length(GetOperationText(NewProgram[NewProgramIndex]))); 139 Inc(FProgramIndex); 140 Inc(NewProgramIndex); 141 end; 142 SetLength(NewProgram, NewProgramIndex); 143 144 // Replace old program by new program 145 SetLength(FProgram, Length(NewProgram)); 146 Move(Pointer(NewProgram)^, Pointer(FProgram)^, SizeOf(TMachineOperation) * Length(NewProgram)); 147 end; 148 189 DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgram.Index, NewTargetIndex); 190 Inc(NewTargetIndex, Length(GetOperationText(NewProgram[NewProgram.Index]))); 191 Inc(FProgramIndex); 192 Inc(NewProgram.Index); 193 end; 194 195 NewProgram.Count := NewProgram.Index; 196 FProgram.Assign(NewProgram); 197 FreeAndNil(NewProgram); 198 end; 199 200 // Converts [-] into mSet,0 (=0) command 201 procedure TBFTarget.OptimizeSetZero; 202 var 203 NewProgram: TProgram; 204 FirstIndex: Integer; 205 NewTargetIndex: Integer; 206 begin 207 NewTargetIndex := 0; 208 NewProgram := TProgram.Create; 209 NewProgram.Count := FProgram.Count; 210 211 FProgramIndex := 0; 212 while FProgramIndex < FProgram.Count do begin 213 FirstIndex := FProgramIndex; 214 case FProgram[FProgramIndex].Command of 215 cmLoopStart: begin 216 if CheckLoopSetZero then begin 217 NewProgram[NewProgram.Index] := TMachineOperation.Create(cmSet, 0, 0); 218 Inc(FProgramIndex, 2); 219 end else begin 220 NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 221 end; 222 end; 223 else NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 224 end; 225 DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgram.Index, NewTargetIndex); 226 Inc(NewTargetIndex, Length(GetOperationText(NewProgram[NewProgram.Index]))); 227 Inc(FProgramIndex); 228 Inc(NewProgram.Index); 229 end; 230 NewProgram.Count := NewProgram.Index; 231 FProgram.Assign(NewProgram); 232 FreeAndNil(NewProgram); 233 end; 234 235 // Merge together cmInc, cmDec, cmSet 236 // Merge together cmPointerInc, cmPointerDec 149 237 procedure TBFTarget.OptimizeMerge; 150 238 var 151 NewProgram: array of TMachineOperation; 152 NewProgramIndex: Integer; 239 NewProgram: TProgram; 153 240 PreviousCommand: TMachineCommand; 154 241 FirstIndex: Integer; 155 242 NewTargetIndex: Integer; 156 243 begin 157 // Merge together cmInc, cmDec, cmSet 158 // Merge together cmPointerInc, cmPointerDec 244 NewTargetIndex := 0; 245 NewProgram := TProgram.Create; 246 NewProgram.Count := FProgram.Count; 247 159 248 PreviousCommand := cmNoOperation; 160 NewProgramIndex := 0;161 SetLength(NewProgram, Length(FProgram));162 163 249 FProgramIndex := 0; 164 NewTargetIndex := 0; 165 while (FProgramIndex < Length(FProgram)) do begin 250 while FProgramIndex < FProgram.Count do begin 166 251 FirstIndex := FProgramIndex; 167 252 case FProgram[FProgramIndex].Command of 168 253 cmPointerInc: begin 169 254 if PreviousCommand in [cmPointerInc, cmPointerDec] then begin 170 if NewProgram[NewProgramIndex - 1].Command = cmPointerInc then 171 NewProgram[NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter + 255 if NewProgram[NewProgram.Index - 1].Command = cmPointerInc then 256 NewProgram.Operations[NewProgram.Index - 1].Parameter := NewProgram[NewProgram.Index - 1].Parameter + 257 FProgram[FProgram.Index].Parameter 258 else 259 if NewProgram[NewProgram.Index - 1].Command = cmPointerDec then 260 NewProgram.Operations[NewProgram.Index - 1].Parameter := NewProgram[NewProgram.Index - 1].Parameter - 261 FProgram[FProgram.Index].Parameter; 262 // If value negative then change command 263 if NewProgram[NewProgram.Index - 1].Parameter < 0 then begin 264 NewProgram.Operations[NewProgram.Index - 1].Parameter := -NewProgram[NewProgram.Index - 1].Parameter; 265 if NewProgram[NewProgram.Index - 1].Command = cmPointerInc then 266 NewProgram.Operations[NewProgram.Index - 1].Command := cmPointerDec 267 else NewProgram.Operations[NewProgram.Index - 1].Command := cmPointerInc; 268 end; 269 if NewProgram.Operations[NewProgram.Index - 1].Parameter = 0 then Dec(NewProgram.Index); 270 Dec(NewProgram.Index); 271 end else begin 272 NewProgram[NewProgram.Index] := TMachineOperation.Create(cmPointerInc, 273 FProgram[FProgramIndex].Parameter); 274 end; 275 end; 276 cmPointerDec: begin 277 if PreviousCommand in [cmPointerInc, cmPointerDec] then begin 278 if NewProgram[NewProgram.Index - 1].Command = cmPointerDec then 279 NewProgram.Operations[NewProgram.Index - 1].Parameter := NewProgram[NewProgram.Index - 1].Parameter + 172 280 FProgram[FProgramIndex].Parameter 173 else if NewProgram[NewProgram Index - 1].Command = cmPointerDec then174 NewProgram [NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter -281 else if NewProgram[NewProgram.Index - 1].Command = cmPointerInc then 282 NewProgram.Operations[NewProgram.Index - 1].Parameter := NewProgram[NewProgram.Index - 1].Parameter - 175 283 FProgram[FProgramIndex].Parameter; 176 284 // If value negative then change command 177 if NewProgram[NewProgram Index - 1].Parameter < 0 then begin178 NewProgram [NewProgramIndex - 1].Parameter := -NewProgram[NewProgramIndex - 1].Parameter;179 if NewProgram[NewProgram Index - 1].Command = cmPointerInc then180 NewProgram [NewProgramIndex - 1].Command := cmPointerDec181 else NewProgram [NewProgramIndex - 1].Command := cmPointerInc;285 if NewProgram[NewProgram.Index - 1].Parameter < 0 then begin 286 NewProgram.Operations[NewProgram.Index - 1].Parameter := -NewProgram[NewProgram.Index - 1].Parameter; 287 if NewProgram[NewProgram.Index - 1].Command = cmPointerInc then 288 NewProgram.Operations[NewProgram.Index - 1].Command := cmPointerDec 289 else NewProgram.Operations[NewProgram.Index - 1].Command := cmPointerInc; 182 290 end; 183 if NewProgram[NewProgram Index - 1].Parameter = 0 then Dec(NewProgramIndex);184 Dec(NewProgram Index);185 end else begin 186 NewProgram[NewProgram Index].Command := cmPointerInc;187 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter;188 end; 189 end; 190 cm PointerDec: begin191 if PreviousCommand in [cm PointerInc, cmPointerDec] then begin192 if NewProgram[NewProgram Index - 1].Command = cmPointerDecthen193 NewProgram [NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter +291 if NewProgram[NewProgram.Index - 1].Parameter = 0 then Dec(NewProgram.Index); 292 Dec(NewProgram.Index); 293 end else begin 294 NewProgram[NewProgram.Index] := TMachineOperation.Create(cmPointerDec, 295 FProgram[FProgramIndex].Parameter); 296 end; 297 end; 298 cmInc: begin 299 if PreviousCommand in [cmInc, cmDec, cmSet] then begin 300 if NewProgram[NewProgram.Index - 1].Command in [cmInc, cmSet] then 301 NewProgram.Operations[NewProgram.Index - 1].Parameter := NewProgram[NewProgram.Index - 1].Parameter + 194 302 FProgram[FProgramIndex].Parameter 195 else if NewProgram[NewProgram Index - 1].Command = cmPointerInc then196 NewProgram [NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter -303 else if NewProgram[NewProgram.Index - 1].Command = cmDec then 304 NewProgram.Operations[NewProgram.Index - 1].Parameter := NewProgram[NewProgram.Index - 1].Parameter - 197 305 FProgram[FProgramIndex].Parameter; 198 306 // If value negative then change command 199 if NewProgram[NewProgramIndex - 1].Parameter < 0then begin200 NewProgram [NewProgramIndex - 1].Parameter := -NewProgram[NewProgramIndex - 1].Parameter;201 if NewProgram[NewProgram Index - 1].Command = cmPointerInc then202 NewProgram [NewProgramIndex - 1].Command := cmPointerDec203 else NewProgram [NewProgramIndex - 1].Command := cmPointerInc;307 if (NewProgram[NewProgram.Index - 1].Parameter < 0) and (NewProgram[NewProgram.Index - 1].Command <> cmSet) then begin 308 NewProgram.Operations[NewProgram.Index - 1].Parameter := -NewProgram[NewProgram.Index - 1].Parameter; 309 if NewProgram[NewProgram.Index - 1].Command = cmInc then 310 NewProgram.Operations[NewProgram.Index - 1].Command := cmDec 311 else NewProgram.Operations[NewProgram.Index - 1].Command := cmInc; 204 312 end; 205 if NewProgram[NewProgram Index - 1].Parameter = 0 then Dec(NewProgramIndex);206 Dec(NewProgram Index);207 end else begin 208 NewProgram[NewProgram Index].Command := cmPointerDec;209 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter;210 end; 211 end; 212 cm Inc: begin313 if NewProgram[NewProgram.Index - 1].Parameter = 0 then Dec(NewProgram.Index); 314 Dec(NewProgram.Index); 315 end else begin 316 NewProgram[NewProgram.Index] := TMachineOperation.Create(cmInc, 317 FProgram[FProgramIndex].Parameter); 318 end; 319 end; 320 cmDec: begin 213 321 if PreviousCommand in [cmInc, cmDec, cmSet] then begin 214 if NewProgram[NewProgram Index - 1].Command in [cmInc, cmSet]then215 NewProgram [NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter +322 if NewProgram[NewProgram.Index - 1].Command = cmDec then 323 NewProgram.Operations[NewProgram.Index - 1].Parameter := NewProgram[NewProgram.Index - 1].Parameter + 216 324 FProgram[FProgramIndex].Parameter 217 else if NewProgram[NewProgram Index - 1].Command = cmDecthen218 NewProgram [NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter -325 else if NewProgram[NewProgram.Index - 1].Command in [cmInc, cmSet] then 326 NewProgram.Operations[NewProgram.Index - 1].Parameter := NewProgram[NewProgram.Index - 1].Parameter - 219 327 FProgram[FProgramIndex].Parameter; 220 328 // If value negative then change command 221 if (NewProgram[NewProgram Index - 1].Parameter < 0) and (NewProgram[NewProgramIndex - 1].Command <> cmSet) then begin222 NewProgram [NewProgramIndex - 1].Parameter := -NewProgram[NewProgramIndex - 1].Parameter;223 if NewProgram[NewProgram Index - 1].Command = cmInc then224 NewProgram [NewProgramIndex - 1].Command := cmDec225 else NewProgram [NewProgramIndex - 1].Command := cmInc;329 if (NewProgram[NewProgram.Index - 1].Parameter < 0) and (NewProgram[NewProgram.Index - 1].Command <> cmSet) then begin 330 NewProgram.Operations[NewProgram.Index - 1].Parameter := -NewProgram[NewProgram.Index - 1].Parameter; 331 if NewProgram[NewProgram.Index - 1].Command = cmInc then 332 NewProgram.Operations[NewProgram.Index - 1].Command := cmDec 333 else NewProgram.Operations[NewProgram.Index - 1].Command := cmInc; 226 334 end; 227 if NewProgram[NewProgramIndex - 1].Parameter = 0 then Dec(NewProgramIndex); 228 Dec(NewProgramIndex); 229 end else begin 230 NewProgram[NewProgramIndex].Command := cmInc; 231 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 232 end; 233 end; 234 cmDec: begin 235 if PreviousCommand in [cmInc, cmDec, cmSet] then begin 236 if NewProgram[NewProgramIndex - 1].Command = cmDec then 237 NewProgram[NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter + 238 FProgram[FProgramIndex].Parameter 239 else if NewProgram[NewProgramIndex - 1].Command in [cmInc, cmSet] then 240 NewProgram[NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter - 241 FProgram[FProgramIndex].Parameter; 242 // If value negative then change command 243 if (NewProgram[NewProgramIndex - 1].Parameter < 0) and (NewProgram[NewProgramIndex - 1].Command <> cmSet) then begin 244 NewProgram[NewProgramIndex - 1].Parameter := -NewProgram[NewProgramIndex - 1].Parameter; 245 if NewProgram[NewProgramIndex - 1].Command = cmInc then 246 NewProgram[NewProgramIndex - 1].Command := cmDec 247 else NewProgram[NewProgramIndex - 1].Command := cmInc; 248 end; 249 if NewProgram[NewProgramIndex - 1].Parameter = 0 then Dec(NewProgramIndex); 250 Dec(NewProgramIndex); 251 end else begin 252 NewProgram[NewProgramIndex].Command := cmDec; 253 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 335 if NewProgram[NewProgram.Index - 1].Parameter = 0 then Dec(NewProgram.Index); 336 Dec(NewProgram.Index); 337 end else begin 338 NewProgram[NewProgram.Index] := TMachineOperation.Create(cmDec, 339 FProgram[FProgramIndex].Parameter); 254 340 end; 255 341 end; … … 257 343 if PreviousCommand in [cmInc, cmDec, cmSet] then begin 258 344 // Set overrides value of previous commands 259 Dec(NewProgramIndex); 260 NewProgram[NewProgramIndex].Command := cmSet; 261 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 262 end else begin 263 NewProgram[NewProgramIndex] := FProgram[FProgramIndex]; 264 end; 265 end; 266 cmLoopStart: begin 267 if CheckClear then begin 268 NewProgram[NewProgramIndex] := TMachineOperation.Create(cmSet, 0, 0); 269 Inc(FProgramIndex, 2); 270 end else begin 271 NewProgram[NewProgramIndex] := FProgram[FProgramIndex]; 272 end; 273 end; 274 else NewProgram[NewProgramIndex] := FProgram[FProgramIndex]; 345 Dec(NewProgram.Index); 346 NewProgram[NewProgram.Index] := TMachineOperation.Create(cmSet, 347 FProgram[FProgramIndex].Parameter); 348 end else begin 349 NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 350 end; 351 end; 352 else NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 275 353 end; 276 354 PreviousCommand := FProgram[FProgramIndex].Command; 277 DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgramIndex, NewTargetIndex); 278 Inc(NewTargetIndex, Length(GetOperationText(NewProgram[NewProgramIndex]))); 279 Inc(FProgramIndex); 280 Inc(NewProgramIndex); 281 end; 282 SetLength(NewProgram, NewProgramIndex); 283 284 // Replace old program by new program 285 SetLength(FProgram, Length(NewProgram)); 286 Move(Pointer(NewProgram)^, Pointer(FProgram)^, SizeOf(TMachineOperation) * Length(NewProgram)); 355 DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgram.Index, NewTargetIndex); 356 Inc(NewTargetIndex, Length(GetOperationText(NewProgram[NewProgram.Index]))); 357 Inc(FProgramIndex); 358 Inc(NewProgram.Index); 359 end; 360 361 NewProgram.Count := NewProgram.Index; 362 FProgram.Assign(NewProgram); 363 FreeAndNil(NewProgram); 287 364 end; 288 365 … … 296 373 procedure TBFTarget.OptimizeRelativeIndexes; 297 374 var 298 NewProgram: array of TMachineOperation; 299 NewProgramIndex: Integer; 375 NewProgram: TProgram; 300 376 RelIndex: Integer; 301 377 FirstIndex: Integer; 302 378 NewTargetIndex: Integer; 303 379 begin 304 NewProgramIndex := 0; 305 SetLength(NewProgram, Length(FProgram)); 380 NewTargetIndex := 0; 381 NewProgram := TProgram.Create; 382 NewProgram.Count := FProgram.Count; 306 383 307 384 RelIndex := 0; 308 385 FProgramIndex := 0; 309 NewTargetIndex := 0; 310 while (FProgramIndex < Length(FProgram)) do begin 386 while FProgramIndex < FProgram.Count do begin 311 387 FirstIndex := FProgramIndex; 312 388 case FProgram[FProgramIndex].Command of 313 389 cmPointerInc: begin 314 390 RelIndex := RelIndex + FProgram[FProgramIndex].Parameter; 315 Dec(NewProgram Index);391 Dec(NewProgram.Index); 316 392 end; 317 393 cmPointerDec: begin 318 394 RelIndex := RelIndex - FProgram[FProgramIndex].Parameter; 319 Dec(NewProgram Index);395 Dec(NewProgram.Index); 320 396 end; 321 397 cmInc, cmDec, cmInput, cmOutput, cmSet: begin 322 NewProgram[NewProgram Index] := FProgram[FProgramIndex];323 NewProgram [NewProgramIndex].RelIndex :=324 NewProgram[NewProgram Index].RelIndex + RelIndex;398 NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 399 NewProgram.Operations[NewProgram.Index].RelIndex := 400 NewProgram[NewProgram.Index].RelIndex + RelIndex; 325 401 end; 326 402 cmLoopStart, cmLoopEnd: begin 327 403 if RelIndex > 0 then begin 328 NewProgram[NewProgram Index] := TMachineOperation.Create(cmPointerInc,404 NewProgram[NewProgram.Index] := TMachineOperation.Create(cmPointerInc, 329 405 RelIndex, 0); 330 Inc(NewProgram Index);406 Inc(NewProgram.Index); 331 407 RelIndex := 0; 332 408 end else 333 409 if RelIndex < 0 then begin 334 NewProgram[NewProgram Index] := TMachineOperation.Create(cmPointerDec,410 NewProgram[NewProgram.Index] := TMachineOperation.Create(cmPointerDec, 335 411 Abs(RelIndex), 0); 336 Inc(NewProgram Index);412 Inc(NewProgram.Index); 337 413 RelIndex := 0; 338 414 end; 339 NewProgram[NewProgram Index] := FProgram[FProgramIndex];415 NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 340 416 end; 341 417 else raise Exception.Create(Format(SUnsupportedCommand, [FProgram[FProgramIndex].Command])); 342 418 end; 343 DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgramIndex, NewTargetIndex); 344 Inc(NewTargetIndex, Length(GetOperationText(NewProgram[NewProgramIndex]))); 345 Inc(FProgramIndex); 346 Inc(NewProgramIndex); 347 end; 348 SetLength(NewProgram, NewProgramIndex); 349 350 // Replace old program by new program 351 SetLength(FProgram, Length(NewProgram)); 352 Move(Pointer(NewProgram)^, Pointer(FProgram)^, SizeOf(TMachineOperation) * 353 Length(NewProgram)); 354 end; 355 419 DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgram.Index, NewTargetIndex); 420 Inc(NewTargetIndex, Length(GetOperationText(NewProgram[NewProgram.Index]))); 421 Inc(FProgramIndex); 422 Inc(NewProgram.Index); 423 end; 424 425 NewProgram.Count := NewProgram.Index; 426 FProgram.Assign(NewProgram); 427 FreeAndNil(NewProgram); 428 end; 429 430 function TBFTarget.CheckLoopDecrementCount: Integer; 431 var 432 I: Integer; 433 PointerChange: Integer; 434 begin 435 Result := 0; 436 PointerChange := 0; 437 I := FProgramIndex + 1; 438 while I < FProgram.Count do begin 439 case FProgram[I].Command of 440 cmPointerInc: begin 441 Inc(PointerChange, FProgram[I].Parameter); 442 end; 443 cmPointerDec: begin 444 Dec(PointerChange, FProgram[I].Parameter); 445 end; 446 cmInc: begin 447 end; 448 cmDec: begin 449 if (PointerChange = 0) and (FProgram[I].RelIndex = 0) and 450 (FProgram[I].Parameter = 1) then 451 Inc(Result); 452 end; 453 cmLoopEnd: begin 454 if (Result = 1) and (PointerChange = 0) then begin 455 Break; 456 end; 457 end; 458 else begin 459 // The loop can't be optimized as there are other operations inside 460 Result := 0; 461 Break; 462 end; 463 end; 464 Inc(I); 465 end; 466 end; 467 468 // Optimize copy and multiply loops like [>+<-] or [>++<-] or [>+>+<<-] 356 469 procedure TBFTarget.OptimizeCopyMultiply; 357 470 var 358 NewProgram: array of TMachineOperation; 359 NewProgramIndex: Integer; 360 ProcessLoop: Boolean; 471 NewProgram: TProgram; 472 ProcessingLoop: Boolean; 361 473 PointerChange: Integer; 362 474 NumberOfBaseDecrement: Integer; 363 LoopStartIndex: Integer;364 LoopStartIndexNew: Integer;365 475 FirstIndex: Integer; 366 476 NewTextIndex: Integer; 367 begin 368 NewProgramIndex := 0; 369 SetLength(NewProgram, Length(FProgram)); 477 NoNewCode: Boolean; 478 begin 479 NewProgram := TProgram.Create; 480 NewProgram.Count := FProgram.Count; 370 481 371 482 NumberOfBaseDecrement := 0; 372 Process Loop := False;483 ProcessingLoop := False; 373 484 FProgramIndex := 0; 374 485 NewTextIndex := 0; 375 486 PointerChange := 0; 376 while (FProgramIndex < Length(FProgram))do begin487 while FProgramIndex < FProgram.Count do begin 377 488 FirstIndex := FProgramIndex; 489 NoNewCode := False; 378 490 case FProgram[FProgramIndex].Command of 379 491 cmPointerInc: begin 380 PointerChange := PointerChange + FProgram[FProgramIndex].Parameter;381 NewProgram[NewProgram Index] := FProgram[FProgramIndex];492 Inc(PointerChange, FProgram[FProgramIndex].Parameter); 493 NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 382 494 end; 383 495 cmPointerDec: begin 384 PointerChange := PointerChange - FProgram[FProgramIndex].Parameter;385 NewProgram[NewProgram Index] := FProgram[FProgramIndex];496 Dec(PointerChange, FProgram[FProgramIndex].Parameter); 497 NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 386 498 end; 387 499 cmInc: begin 388 if not Process Loop then begin389 NewProgram[NewProgram Index] := FProgram[FProgramIndex];500 if not ProcessingLoop then begin 501 NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 390 502 end else begin 391 503 if ((FProgram[FProgramIndex].RelIndex + PointerChange) <> 0) then begin 392 NewProgram[NewProgram Index] := FProgram[FProgramIndex];393 NewProgram [NewProgramIndex].Command := cmMultipy;394 end else Dec(NewProgramIndex);504 NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 505 NewProgram.Operations[NewProgram.Index].Command := cmMultiply; 506 end else NoNewCode := True; 395 507 end; 396 508 end; 397 509 cmDec: begin 398 if not Process Loop then begin510 if not ProcessingLoop then begin 399 511 if (PointerChange = 0) and (FProgram[FProgramIndex].RelIndex = 0) and 400 512 (FProgram[FProgramIndex].Parameter = 1) then 401 513 Inc(NumberOfBaseDecrement); 402 NewProgram[NewProgram Index] := FProgram[FProgramIndex];514 NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 403 515 end else begin 404 516 if ((FProgram[FProgramIndex].RelIndex + PointerChange) <> 0) then begin 405 NewProgram[NewProgram Index] := FProgram[FProgramIndex];406 NewProgram [NewProgramIndex].Command := cmMultipy;407 NewProgram [NewProgramIndex].Parameter := -FProgram[FProgramIndex].Parameter;408 end else Dec(NewProgramIndex);517 NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 518 NewProgram.Operations[NewProgram.Index].Command := cmMultiply; 519 NewProgram.Operations[NewProgram.Index].Parameter := -FProgram[FProgramIndex].Parameter; 520 end else NoNewCode := True; 409 521 end; 410 522 end; 411 523 cmInput, cmOutput: begin 412 NewProgram[NewProgram Index] := FProgram[FProgramIndex];524 NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 413 525 Inc(NumberOfBaseDecrement, 2); 414 526 end; 415 527 cmSet: begin 416 NewProgram[NewProgram Index] := FProgram[FProgramIndex];528 NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 417 529 Inc(NumberOfBaseDecrement, 2); 418 530 end; 419 531 cmLoopStart: begin 420 if not ProcessLoop then begin 421 NumberOfBaseDecrement := 0; 422 PointerChange := 0; 423 LoopStartIndex := FProgramIndex; 424 LoopStartIndexNew := NewProgramIndex; 425 NewProgram[NewProgramIndex] := FProgram[FProgramIndex]; 426 end else begin 427 Dec(NewProgramIndex); 532 if not ProcessingLoop then begin 533 if CheckLoopDecrementCount = 1 then begin 534 PointerChange := 0; 535 ProcessingLoop := True; 536 NoNewCode := True; 537 end else 538 NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 539 end else begin 540 NoNewCode := True; 428 541 end; 429 542 end; 430 543 cmLoopEnd: begin 431 if not ProcessLoop then begin 432 if (NumberOfBaseDecrement = 1) and (PointerChange = 0) then begin 433 FProgramIndex := LoopstartIndex - 1; 434 NewProgramIndex := LoopStartIndexNew - 1; 435 ProcessLoop := True; 436 end else begin 437 NewProgram[NewProgramIndex] := FProgram[FProgramIndex]; 438 end; 439 end else begin 440 NewProgram[NewProgramIndex] := TMachineOperation.Create(cmSet, 0, 0); 441 ProcessLoop := False; 442 NumberOfBaseDecrement := 0; 544 if not ProcessingLoop then begin 545 NewProgram[NewProgram.Index] := FProgram[FProgramIndex]; 546 end else begin 547 // Finally set decrementing cell to zero 548 NewProgram[NewProgram.Index] := TMachineOperation.Create(cmSet, 0, 0); 549 ProcessingLoop := False; 443 550 end; 444 551 end; 445 552 else raise Exception.Create(Format(SUnsupportedCommand, [FProgram[FProgramIndex].Command])); 446 553 end; 447 DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgramIndex, NewTextIndex); 448 Inc(NewTextIndex, Length(GetOperationText(NewProgram[NewProgramIndex]))); 449 Inc(FProgramIndex); 450 Inc(NewProgramIndex); 451 end; 452 SetLength(NewProgram, NewProgramIndex); 453 454 // Replace old program by new program 455 SetLength(FProgram, Length(NewProgram)); 456 Move(Pointer(NewProgram)^, Pointer(FProgram)^, SizeOf(TMachineOperation) * 457 Length(NewProgram)); 554 if NoNewCode then DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, -1, NewTextIndex) 555 else begin 556 DebugSteps.UpdateTargetPos(FirstIndex, FProgramIndex, NewProgram.Index, NewTextIndex); 557 Inc(NewTextIndex, Length(GetOperationText(NewProgram[NewProgram.Index]))); 558 end; 559 Inc(FProgramIndex); 560 if not NoNewCode then Inc(NewProgram.Index); 561 end; 562 563 NewProgram.Count := NewProgram.Index; 564 FProgram.Assign(NewProgram); 565 FreeAndNil(NewProgram); 458 566 end; 459 567 … … 462 570 Result := BrainFuckCommandText[Operation.Command]; 463 571 if Operation.Command in [cmInc, cmDec, cmPointerInc, cmPointerDec, 464 cmSet, cmMultip y] then begin572 cmSet, cmMultiply] then begin 465 573 if Operation.Parameter <> 1 then 466 574 Result := Result + IntToStr(Operation.Parameter); … … 476 584 inherited; 477 585 DebugSteps.Clear; 478 SetLength(FProgram, Length(FSourceCode));586 FProgram.Count := Length(FSourceCode); 479 587 FProgramIndex := 0; 480 588 for I := 1 to Length(FSourceCode) do begin … … 516 624 Inc(FProgramIndex); 517 625 end; 518 SetLength(FProgram, FProgramIndex);626 FProgram.Count := FProgramIndex; 519 627 end; 520 628 … … 524 632 MemorySize := 30000; 525 633 CellSize := 256; 634 FProgram := TProgram.Create; 635 end; 636 637 destructor TBFTarget.Destroy; 638 begin 639 FreeAndNil(FProgram); 640 inherited; 526 641 end; 527 642 … … 532 647 inherited; 533 648 if Optimizations.AddSub then OptimizeAddSub; 649 if Optimizations.SetZero then OptimizeSetZero; 534 650 if Optimizations.Merge then 535 651 repeat 536 OldLength := Length(FProgram);652 OldLength := FProgram.Count; 537 653 OptimizeMerge; 538 until Length(FProgram)= OldLength;654 until FProgram.Count = OldLength; 539 655 OptimizeZeroInitMemory; 540 656 if Optimizations.RelativeIndexes then OptimizeRelativeIndexes; -
trunk/UTarget.pas
r125 r126 289 289 Last := SearchIndexByProgramPos(OldProgramTo); 290 290 for I := Last downto First + 1 do Delete(I); 291 Items[First].ProgramPosition := NewProgram; 292 Items[First].TargetPosition := NewTarget; 293 end; 294 291 if NewProgram = -1 then begin 292 Delete(First); 293 end else begin 294 if (First >= 0) and (First < Count) then begin 295 Items[First].ProgramPosition := NewProgram; 296 Items[First].TargetPosition := NewTarget; 297 end else begin 298 // Index not found, possible new command? 299 end; 300 end; 301 end; 295 302 296 303 { TTargetList }
Note:
See TracChangeset
for help on using the changeset viewer.