Changeset 64
- Timestamp:
- Dec 4, 2014, 2:59:28 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 1 added
- 16 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Forms/UFormMain.pas
r63 r64 194 194 FCurrentTarget.OnChangeState := TargetStateChanged; 195 195 FCurrentTarget.OnLog := TargetLogExecute; 196 FCurrentTarget.OptimizationLevel := Core.OptimizationLevel; 196 197 FCurrentTarget.Messages.OnChange := MessagesChanged; 197 198 end; … … 469 470 //OptionsForm.SaveToInterpretter(CurrentTarget); 470 471 OptionsForm.Save; 472 if Assigned(CurrentTarget) then 473 CurrentTarget.OptimizationLevel := Core.OptimizationLevel; 471 474 end; 472 475 end; … … 476 479 with CurrentTarget do begin 477 480 AProgramStop.Execute; 478 Optimization := coNormal;479 481 SourceCode := FormSourceCode.MemoSource.Text; 480 482 ProjectFileName := Core.Project.FileName; -
trunk/Forms/UFormOptions.lfm
r59 r64 1 1 object OptionsForm: TOptionsForm 2 2 Left = 415 3 Height = 2793 Height = 339 4 4 Top = 210 5 5 Width = 468 6 6 Caption = 'Options' 7 ClientHeight = 2797 ClientHeight = 339 8 8 ClientWidth = 468 9 9 OnShow = FormShow … … 12 12 Left = 389 13 13 Height = 25 14 Top = 24714 Top = 307 15 15 Width = 75 16 16 Anchors = [akRight, akBottom] … … 23 23 Left = 304 24 24 Height = 25 25 Top = 24725 Top = 307 26 26 Width = 75 27 27 Anchors = [akRight, akBottom] … … 80 80 TabOrder = 4 81 81 end 82 object ComboBoxLanguage: TComboBox83 Left = 24884 Height = 3785 Top = 8386 Width = 18887 ItemHeight = 088 Style = csDropDownList89 TabOrder = 590 end91 82 object SpinEditDPIX: TSpinEdit 92 83 Left = 104 … … 96 87 MaxValue = 1000 97 88 MinValue = 1 98 TabOrder = 689 TabOrder = 5 99 90 Value = 1 100 91 end … … 114 105 MaxValue = 1000 115 106 MinValue = 1 116 TabOrder = 7107 TabOrder = 6 117 108 Value = 1 118 109 end … … 134 125 Caption = 'Automatic DPI' 135 126 OnChange = CheckBoxDPIAutoChange 127 TabOrder = 7 128 end 129 object Label4: TLabel 130 Left = 16 131 Height = 25 132 Top = 248 133 Width = 221 134 Caption = 'Compiler optimizations:' 135 ParentColor = False 136 end 137 object ComboBoxLanguage: TComboBox 138 Left = 248 139 Height = 37 140 Top = 83 141 Width = 188 142 ItemHeight = 0 143 Style = csDropDownList 136 144 TabOrder = 8 137 145 end 146 object ComboBoxOptimizatipn: TComboBox 147 Left = 256 148 Height = 37 149 Top = 240 150 Width = 188 151 ItemHeight = 0 152 Items.Strings = ( 153 'None' 154 'Normal' 155 ) 156 Style = csDropDownList 157 TabOrder = 9 158 end 138 159 end -
trunk/Forms/UFormOptions.lrt
r59 r64 9 9 TOPTIONSFORM.LABELX.CAPTION=x 10 10 TOPTIONSFORM.CHECKBOXDPIAUTO.CAPTION=Automatic DPI 11 TOPTIONSFORM.LABEL4.CAPTION=Compiler optimizations: -
trunk/Forms/UFormOptions.pas
r59 r64 7 7 uses 8 8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls, 9 Spin, UTargetInterpretter ;9 Spin, UTargetInterpretter, UTarget; 10 10 11 11 type … … 19 19 CheckBoxDPIAuto: TCheckBox; 20 20 ComboBoxLanguage: TComboBox; 21 ComboBoxOptimizatipn: TComboBox; 21 22 Label1: TLabel; 22 23 Label2: TLabel; 23 24 Label3: TLabel; 25 Label4: TLabel; 24 26 LabelDPI: TLabel; 25 27 LabelX: TLabel; … … 78 80 SpinEditDPIY.Value := Core.ScaleDPI.DPI.Y; 79 81 CheckBoxDPIAuto.Checked := Core.ScaleDPI.AutoDetect; 82 ComboBoxOptimizatipn.ItemIndex := Integer(Core.OptimizationLevel); 80 83 UpdateInterface; 81 84 end; … … 86 89 Core.ScaleDPI.DPI.Y := SpinEditDPIY.Value; 87 90 Core.ScaleDPI.AutoDetect := CheckBoxDPIAuto.Checked; 91 Core.OptimizationLevel := TCompilerOptimization(ComboBoxOptimizatipn.ItemIndex); 88 92 end; 89 93 -
trunk/Forms/UFormTargetCode.lfm
r48 r64 7 7 ClientHeight = 240 8 8 ClientWidth = 320 9 LCLVersion = '1. 1'9 LCLVersion = '1.3' 10 10 object MemoTarget: TMemo 11 11 Left = 0 … … 14 14 Width = 320 15 15 Align = alClient 16 Font.Name = 'Courier New' 17 ParentFont = False 16 18 PopupMenu = PopupMenuTarget 17 19 ReadOnly = True -
trunk/Languages/LazFuckIDE.cs.po
r62 r64 547 547 msgstr "Jazyk rozhranÃ:" 548 548 549 #: toptionsform.label4.caption 550 msgid "Compiler optimizations:" 551 msgstr "" 552 549 553 #: toptionsform.labeldpi.caption 550 554 msgctxt "toptionsform.labeldpi.caption" … … 743 747 msgid "Read input error" 744 748 msgstr "Chyba Ätenà vstupu" 749 750 #: utargetinterpretter.sunsupportedcommand 751 msgid "Unsupported command" 752 msgstr "" 753 -
trunk/Languages/LazFuckIDE.po
r62 r64 534 534 msgstr "" 535 535 536 #: toptionsform.label4.caption 537 msgid "Compiler optimizations:" 538 msgstr "" 539 536 540 #: toptionsform.labeldpi.caption 537 541 msgctxt "TOPTIONSFORM.LABELDPI.CAPTION" … … 731 735 msgstr "" 732 736 737 #: utargetinterpretter.sunsupportedcommand 738 msgid "Unsupported command" 739 msgstr "" 740 -
trunk/LazFuckIDE.lpi
r61 r64 133 133 </Unit4> 134 134 <Unit5> 135 <Filename Value=" Target\UTarget.pas"/>135 <Filename Value="UTarget.pas"/> 136 136 <IsPartOfProject Value="True"/> 137 137 <UnitName Value="UTarget"/> … … 207 207 <HasResources Value="True"/> 208 208 <ResourceBaseClass Value="Form"/> 209 <UnitName Value="UFormTargetCode"/> 209 210 </Unit16> 210 211 <Unit17> -
trunk/Target/UTargetC.pas
r60 r64 65 65 FProgramIndex := 0; 66 66 while (FProgramIndex < Length(FProgram)) do begin 67 case FProgram[FProgramIndex] of 68 cmPointerInc: begin 69 Sum := CheckOccurence(cmPointerInc); 70 AddLine('Pos = Pos + ' + IntToStr(Sum) + ';'); 71 end; 72 cmPointerDec: begin 73 Sum := CheckOccurence(cmPointerDec); 74 AddLine('Pos = Pos - ' + IntToStr(Sum) + ';'); 75 end; 76 cmInc: begin 77 Sum := CheckOccurence(cmInc); 78 AddLine('Memory[Pos] = Memory[Pos] + ' + IntToStr(Sum) + ';'); 79 end; 80 cmDec: begin 81 Sum := CheckOccurence(cmDec); 82 AddLine('Memory[Pos] = Memory[Pos] - ' + IntToStr(Sum) + ';'); 83 end; 67 case FProgram[FProgramIndex].Command of 68 cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 69 cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 70 cmInc: AddLine('Memory[Pos] = Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 71 cmDec: AddLine('Memory[Pos] = Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 84 72 cmOutput: AddLine('putchar(Memory[Pos]);'); 85 73 cmInput: AddLine('Memory[Pos] = getchar();'); 74 cmSet: AddLine('Memory[Pos] = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 86 75 cmLoopStart: begin 87 if CheckClear then begin 88 AddLine('Memory[Pos] = 0;'); 89 Inc(FProgramIndex, 2); 90 end else begin 91 AddLine('while(Memory[Pos] != 0)'); 92 AddLine('{'); 93 Inc(Indent); 94 end; 76 AddLine('while(Memory[Pos] != 0)'); 77 AddLine('{'); 78 Inc(Indent); 95 79 end; 96 80 cmLoopEnd: begin -
trunk/Target/UTargetDelphi.pas
r60 r64 59 59 FProgramIndex := 0; 60 60 while (FProgramIndex < Length(FProgram)) do begin 61 case FProgram[FProgramIndex] of 62 cmPointerInc: begin 63 Sum := CheckOccurence(cmPointerInc); 64 AddLine('Inc(Pos, ' + IntToStr(Sum) + ');'); 65 end; 66 cmPointerDec: begin 67 Sum := CheckOccurence(cmPointerDec); 68 AddLine('Dec(Pos, ' + IntToStr(Sum) + ');'); 69 end; 70 cmInc: begin 71 Sum := CheckOccurence(cmInc); 72 AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(Sum) + ';'); 73 end; 74 cmDec: begin 75 Sum := CheckOccurence(cmDec); 76 AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(Sum) + ';'); 77 end; 61 case FProgram[FProgramIndex].Command of 62 cmPointerInc: AddLine('Inc(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 63 cmPointerDec: AddLine('Dec(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 64 cmInc: AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 65 cmDec: AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 66 cmSet: AddLine('Memory[Pos] := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 78 67 cmOutput: AddLine('Write(Chr(Memory[Pos]));'); 79 68 cmInput: AddLine('Read(ReadChar); Memory[Pos] := Ord(ReadChar);'); 80 69 cmLoopStart: begin 81 if CheckClear then begin 82 AddLine('Memory[Pos] := 0;'); 83 Inc(FProgramIndex, 2); 84 end else begin 85 AddLine('while Memory[Pos] <> 0 do begin'); 86 Inc(Indent); 87 end; 70 AddLine('while Memory[Pos] <> 0 do begin'); 71 Inc(Indent); 88 72 end; 89 73 cmLoopEnd: begin -
trunk/Target/UTargetFPC.pas
r60 r64 44 44 45 45 procedure TTargetFPC.Compile; 46 var47 Sum: Integer;48 46 begin 49 47 inherited; … … 62 60 FProgramIndex := 0; 63 61 while (FProgramIndex < Length(FProgram)) do begin 64 case FProgram[FProgramIndex] of 65 cmPointerInc: begin 66 Sum := CheckOccurence(cmPointerInc); 67 AddLine('Inc(Pos, ' + IntToStr(Sum) + ');'); 68 end; 69 cmPointerDec: begin 70 Sum := CheckOccurence(cmPointerDec); 71 AddLine('Dec(Pos, ' + IntToStr(Sum) + ');'); 72 end; 73 cmInc: begin 74 Sum := CheckOccurence(cmInc); 75 AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(Sum) + ';'); 76 end; 77 cmDec: begin 78 Sum := CheckOccurence(cmDec); 79 AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(Sum) + ';'); 80 end; 62 case FProgram[FProgramIndex].Command of 63 cmPointerInc: AddLine('Inc(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 64 cmPointerDec: AddLine('Dec(Pos, ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 65 cmInc: AddLine('Memory[Pos] := Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 66 cmDec: AddLine('Memory[Pos] := Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 67 cmSet: AddLine('Memory[Pos] := ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 81 68 cmOutput: AddLine('Write(Chr(Memory[Pos]));'); 82 69 cmInput: AddLine('Read(ReadChar); Memory[Pos] := Ord(ReadChar);'); 83 70 cmLoopStart: begin 84 if CheckClear then begin 85 AddLine('Memory[Pos] := 0;'); 86 Inc(FProgramIndex, 2); 87 end else begin 88 AddLine('while Memory[Pos] <> 0 do begin'); 89 Inc(Indent); 90 end; 71 AddLine('while Memory[Pos] <> 0 do begin'); 72 Inc(Indent); 91 73 end; 92 74 cmLoopEnd: begin -
trunk/Target/UTargetInterpretter.pas
r52 r64 33 33 FThread: TTargetInterpretterThread; 34 34 FStepCount: Integer; 35 FCommandTable: array[T BrainFuckCommand] of TCommandHandler;35 FCommandTable: array[TMachineCommand] of TCommandHandler; 36 36 function GetMemorySize: Integer; 37 37 procedure SetMemorySize(AValue: Integer); … … 46 46 procedure CommandLoopStart; 47 47 procedure CommandLoopEnd; 48 procedure CommandSet; 48 49 procedure PrepareBreakPoints; 49 50 protected … … 81 82 82 83 const 83 BrainFuckCommandText: array[T BrainFuckCommand] of Char = (84 ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@' );84 BrainFuckCommandText: array[TMachineCommand] of Char = ( 85 ' ', '+', '-', '>', '<', '.', ',', '[', ']', '@', '='); 85 86 86 87 … … 95 96 SMemoryCellOutOfRange = 'Memory cell %s value out of range'; 96 97 SProgramNotRunning = 'Program not running'; 98 SUnsupportedCommand = 'Unsupported command'; 97 99 98 100 { TTargetInterpretterThread } … … 111 113 SetStateSafe(rsPaused); 112 114 end else begin 113 FCommandTable[FProgram[FProgramIndex]]; 115 if Assigned(FCommandTable[FProgram[FProgramIndex].Command]) then 116 FCommandTable[FProgram[FProgramIndex].Command] 117 else raise Exception.Create(SUnsupportedCommand); 114 118 Inc(FProgramIndex); 115 119 Inc(FStepCount); … … 177 181 SetLength(Loop, 0); 178 182 for I := 0 to Length(FProgram) - 1 do begin 179 case FProgram[I] of183 case FProgram[I].Command of 180 184 cmLoopStart: begin 181 185 SetLength(Loop, Length(Loop) + 1); … … 194 198 end; 195 199 196 procedure TTargetInterpretter.CommandInc;197 begin198 Memory[MemoryPosition] := ((Memory[MemoryPosition] + 1) mod CellSize);199 end;200 201 procedure TTargetInterpretter.CommandDec;202 begin203 Memory[MemoryPosition] := ((Memory[MemoryPosition] - 1) mod CellSize);204 end;205 206 procedure TTargetInterpretter.CommandPointerInc;207 begin208 if MemoryPosition < MemorySize then Inc(MemoryPosition)209 else raise Exception.Create(SProgramUpperLimit);210 end;211 212 procedure TTargetInterpretter.CommandPointerDec;213 begin214 if MemoryPosition > 0 then Dec(MemoryPosition)215 else raise Exception.Create(SProgramLowerLimit);216 end;217 218 200 procedure TTargetInterpretter.CommandInput; 219 201 begin … … 245 227 if Memory[MemoryPosition] > 0 then 246 228 FProgramIndex := SourceJump[FProgramIndex] - 1; 229 end; 230 231 procedure TTargetInterpretter.CommandInc; 232 begin 233 Memory[MemoryPosition] := ((Memory[MemoryPosition] + FProgram[FProgramIndex].Parameter) mod CellSize); 234 end; 235 236 procedure TTargetInterpretter.CommandDec; 237 begin 238 Memory[MemoryPosition] := ((Memory[MemoryPosition] - FProgram[FProgramIndex].Parameter) mod CellSize); 239 end; 240 241 procedure TTargetInterpretter.CommandPointerInc; 242 begin 243 if MemoryPosition < MemorySize then Inc(MemoryPosition, FProgram[FProgramIndex].Parameter) 244 else raise Exception.Create(SProgramUpperLimit); 245 end; 246 247 procedure TTargetInterpretter.CommandPointerDec; 248 begin 249 if MemoryPosition > 0 then Dec(MemoryPosition, FProgram[FProgramIndex].Parameter) 250 else raise Exception.Create(SProgramLowerLimit); 251 end; 252 253 procedure TTargetInterpretter.CommandSet; 254 begin 255 Memory[MemoryPosition] := FProgram[FProgramIndex].Parameter mod CellSize; 247 256 end; 248 257 … … 286 295 I: Integer; 287 296 begin 288 SetLength(Result, Length(FProgram)); 289 for I := 0 to Length(FProgram) - 1 do 290 Result[I + 1] := BrainFuckCommandText[FProgram[I]]; 297 Result := ''; 298 for I := 0 to Length(FProgram) - 1 do begin 299 Result := Result + BrainFuckCommandText[FProgram[I].Command]; 300 if FProgram[I].Command in [cmInc, cmDec, cmSet, cmPointerInc, cmPointerDec] then begin 301 if FProgram[I].Parameter > 1 then 302 Result := Result + IntToStr(FProgram[I].Parameter); 303 end; 304 end; 291 305 end; 292 306 … … 402 416 MemorySize := 30000; 403 417 CellSize := 256; 418 // Base commands 404 419 FCommandTable[cmInc] := CommandInc; 405 420 FCommandTable[cmDec] := CommandDec; … … 410 425 FCommandTable[cmLoopStart] := CommandLoopStart; 411 426 FCommandTable[cmLoopEnd] := CommandLoopEnd; 427 // Extended commands 428 FCommandTable[cmSet] := CommandSet; 412 429 end; 413 430 -
trunk/Target/UTargetJava.pas
r60 r64 66 66 FProgramIndex := 0; 67 67 while (FProgramIndex < Length(FProgram)) do begin 68 case FProgram[FProgramIndex] of 69 cmPointerInc: begin 70 Sum := CheckOccurence(cmPointerInc); 71 AddLine('Pos = Pos + ' + IntToStr(Sum) + ';'); 72 end; 73 cmPointerDec: begin 74 Sum := CheckOccurence(cmPointerDec); 75 AddLine('Pos = Pos - ' + IntToStr(Sum) + ';'); 76 end; 77 cmInc: begin 78 Sum := CheckOccurence(cmInc); 79 AddLine('Memory[Pos] = (char)((int)Memory[Pos] + ' + IntToStr(Sum) + ');'); 80 end; 81 cmDec: begin 82 Sum := CheckOccurence(cmDec); 83 AddLine('Memory[Pos] = (char)((int)Memory[Pos] - ' + IntToStr(Sum) + ');'); 84 end; 68 case FProgram[FProgramIndex].Command of 69 cmPointerInc: AddLine('Pos = Pos + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 70 cmPointerDec: AddLine('Pos = Pos - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 71 cmInc: AddLine('Memory[Pos] = (char)((int)Memory[Pos] + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 72 cmDec: AddLine('Memory[Pos] = (char)((int)Memory[Pos] - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 85 73 cmOutput: AddLine('System.out.print(Memory[Pos]);'); 86 74 cmInput: AddLine('Memory[Pos] = (char)System.in.read();'); 75 cmSet: AddLine('Memory[Pos] = ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 87 76 cmLoopStart: begin 88 if CheckClear then begin 89 AddLine('Memory[Pos] = 0;'); 90 Inc(FProgramIndex, 2); 91 end else begin 92 AddLine('while(Memory[Pos] != 0)'); 93 AddLine('{'); 94 Inc(Indent); 95 end; 77 AddLine('while(Memory[Pos] != 0)'); 78 AddLine('{'); 79 Inc(Indent); 96 80 end; 97 81 cmLoopEnd: begin -
trunk/Target/UTargetPHP.pas
r60 r64 58 58 FProgramIndex := 0; 59 59 while (FProgramIndex < Length(FProgram)) do begin 60 case FProgram[FProgramIndex] of 61 cmPointerInc: begin 62 Sum := CheckOccurence(cmPointerInc); 63 AddLine('$Position = $Position + ' + IntToStr(Sum) + ';'); 64 end; 65 cmPointerDec: begin 66 Sum := CheckOccurence(cmPointerDec); 67 AddLine('$Position = $Position - ' + IntToStr(Sum) + ';'); 68 end; 69 cmInc: begin 70 Sum := CheckOccurence(cmInc); 71 AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) + ' + IntToStr(Sum) + ');'); 72 end; 73 cmDec: begin 74 Sum := CheckOccurence(cmDec); 75 AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) - ' + IntToStr(Sum) + ');'); 76 end; 60 case FProgram[FProgramIndex].Command of 61 cmPointerInc: AddLine('$Position = $Position + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 62 cmPointerDec: AddLine('$Position = $Position - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ';'); 63 cmInc: AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) + ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 64 cmDec: AddLine('$Memory[$Position] = chr(ord($Memory[$Position]) - ' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 77 65 cmOutput: AddLine('echo($Memory[$Position]);'); 78 66 cmInput: AddLine('$Memory[$Position] = fgetc(STDIN);'); 67 cmSet: AddLine('$Memory[$Position] = chr(' + IntToStr(FProgram[FProgramIndex].Parameter) + ');'); 79 68 cmLoopStart: begin 80 if CheckClear then begin 81 AddLine('$Memory[$Position] = "\0";'); 82 Inc(FProgramIndex, 2); 83 end else begin 84 AddLine('while($Memory[$Position] != "\0") {'); 85 Inc(Indent); 86 end; 69 AddLine('while($Memory[$Position] != "\0") {'); 70 Inc(Indent); 87 71 end; 88 72 cmLoopEnd: begin -
trunk/UCore.pas
r61 r64 25 25 Targets: TTargetList; 26 26 OpenProjectOnStart: Boolean; 27 OptimizationLevel: TCompilerOptimization; 27 28 procedure LoadFromRegistry(Root: HKEY; Key: string); 28 29 procedure SaveToRegistry(Root: HKEY; Key: string); … … 80 81 ReadIntegerWithDefault('DPIY', 96)); 81 82 ScaleDPI.AutoDetect := ReadBoolWithDefault('DPIAuto', True); 83 OptimizationLevel := TCompilerOptimization(ReadIntegerWithDefault('OptimizationLevel', Integer(coNormal))); 82 84 finally 83 85 Free; … … 99 101 WriteInteger('DPIX', ScaleDPI.DPI.X); 100 102 WriteInteger('DPIY', ScaleDPI.DPI.Y); 103 WriteInteger('OptimizationLevel', Integer(OptimizationLevel)); 101 104 if Assigned(CoolTranslator1.Language) and (CoolTranslator1.Language.Code <> '') then 102 105 WriteString('LanguageCode', CoolTranslator1.Language.Code) -
trunk/UTarget.pas
r63 r64 66 66 end; 67 67 68 TBrainFuckCommand = (cmNoOperation, cmInc, cmDec, cmPointerInc, cmPointerDec, 69 cmOutput, cmInput, cmLoopStart, cmLoopEnd, cmDebug); 68 TMachineCommand = (cmNoOperation, cmInc, cmDec, cmPointerInc, cmPointerDec, 69 cmOutput, cmInput, cmLoopStart, cmLoopEnd, cmDebug, cmSet); 70 71 TMachineOperation = record 72 Command: TMachineCommand; 73 Parameter: Integer; 74 end; 70 75 71 76 TLogEvent = procedure (Lines: TStrings) of object; … … 80 85 function SourceReadNext: Char; 81 86 function CheckClear: Boolean; 82 function CheckOccurence(C: TBrainFuckCommand): Integer; 87 function CheckOccurenceSumParam(C: TMachineCommand): Integer; 88 function CheckOccurence(C: TMachineCommand): Integer; 89 procedure OptimizeAddSub; 90 procedure OptimizeMerge; 91 procedure OptimizeZeroInitMemory; 83 92 protected 84 93 FSourceCode: string; 85 FProgram: array of T BrainFuckCommand;94 FProgram: array of TMachineOperation; 86 95 FProgramIndex: Integer; 87 96 FTargetCode: string; … … 101 110 ProgramName: string; 102 111 ImageIndex: Integer; 103 Optimization : TCompilerOptimization;112 OptimizationLevel: TCompilerOptimization; 104 113 CompilerPath: string; 105 114 ExecutorPath: string; … … 367 376 begin 368 377 inherited; 369 Optimization := coNormal;378 OptimizationLevel := coNormal; 370 379 BreakPoints := TBreakPointList.Create; 371 380 DebugSteps := TDebugStepList.Create; … … 387 396 388 397 procedure TTarget.OptimizeSource; 389 begin 390 // Remove redundand code 391 398 var 399 OldLength: Integer; 400 begin 401 OptimizeAddSub; 402 repeat 403 OldLength := Length(FProgram); 404 OptimizeMerge; 405 until Length(FProgram) = OldLength; 406 OptimizeZeroInitMemory; 392 407 end; 393 408 … … 395 410 begin 396 411 LoadProgram; 412 if OptimizationLevel = coNormal then OptimizeSource; 397 413 Compiled := True; 398 414 end; … … 528 544 end; 529 545 530 function TTarget.CheckOccurence(C: T BrainFuckCommand): Integer;546 function TTarget.CheckOccurence(C: TMachineCommand): Integer; 531 547 begin 532 548 Result := 1; 533 if Optimization = coNormal then 534 while ((FProgramIndex + 1) < Length(FProgram)) and (FProgram[FProgramIndex + 1] = C) do begin 549 while ((FProgramIndex + 1) < Length(FProgram)) and (FProgram[FProgramIndex + 1].Command = C) do begin 535 550 Inc(Result); 536 551 Inc(FProgramIndex); 537 552 end; 553 end; 554 555 function TTarget.CheckOccurenceSumParam(C: TMachineCommand): Integer; 556 begin 557 Result := FProgram[FProgramIndex].Parameter; 558 while ((FProgramIndex + 1) < Length(FProgram)) and (FProgram[FProgramIndex + 1].Command = C) do begin 559 Inc(Result, FProgram[FProgramIndex + 1].Parameter); 560 Inc(FProgramIndex); 561 end; 562 end; 563 564 procedure TTarget.OptimizeAddSub; 565 var 566 NewProgram: array of TMachineOperation; 567 NewProgramIndex: Integer; 568 begin 569 NewProgramIndex := 0; 570 SetLength(NewProgram, Length(FProgram)); 571 572 FProgramIndex := 0; 573 while (FProgramIndex < Length(FProgram)) do begin 574 case FProgram[FProgramIndex].Command of 575 cmPointerInc: begin 576 NewProgram[NewProgramIndex].Command := cmPointerInc; 577 NewProgram[NewProgramIndex].Parameter := CheckOccurenceSumParam(cmPointerInc); 578 end; 579 cmPointerDec: begin 580 NewProgram[NewProgramIndex].Command := cmPointerDec; 581 NewProgram[NewProgramIndex].Parameter := CheckOccurenceSumParam(cmPointerDec); 582 end; 583 cmInc: begin 584 NewProgram[NewProgramIndex].Command := cmInc; 585 NewProgram[NewProgramIndex].Parameter := CheckOccurenceSumParam(cmInc); 586 end; 587 cmDec: begin 588 NewProgram[NewProgramIndex].Command := cmDec; 589 NewProgram[NewProgramIndex].Parameter := CheckOccurenceSumParam(cmDec); 590 end; 591 else begin 592 NewProgram[NewProgramIndex].Command := FProgram[FProgramIndex].Command; 593 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 594 end; 595 end; 596 Inc(FProgramIndex); 597 Inc(NewProgramIndex); 598 end; 599 SetLength(NewProgram, NewProgramIndex); 600 601 // Replace old program by new program 602 SetLength(FProgram, Length(NewProgram)); 603 Move(NewProgram[0], FProgram[0], SizeOf(TMachineOperation) * Length(NewProgram)); 604 end; 605 606 procedure TTarget.OptimizeMerge; 607 var 608 NewProgram: array of TMachineOperation; 609 NewProgramIndex: Integer; 610 PreviousCommand: TMachineCommand; 611 begin 612 // Merge together cmInc, cmDec, cmSet 613 // Merge together cmPointerInc, cmPointerDec 614 PreviousCommand := cmNoOperation; 615 NewProgramIndex := 0; 616 SetLength(NewProgram, Length(FProgram)); 617 618 FProgramIndex := 0; 619 while (FProgramIndex < Length(FProgram)) do begin 620 case FProgram[FProgramIndex].Command of 621 cmPointerInc: begin 622 if PreviousCommand in [cmPointerInc, cmPointerDec] then begin 623 if NewProgram[NewProgramIndex - 1].Command = cmPointerInc then 624 NewProgram[NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter + 625 FProgram[FProgramIndex].Parameter 626 else if NewProgram[NewProgramIndex - 1].Command = cmPointerDec then 627 NewProgram[NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter - 628 FProgram[FProgramIndex].Parameter; 629 // If value negative then change command 630 if NewProgram[NewProgramIndex - 1].Parameter < 0 then begin 631 NewProgram[NewProgramIndex - 1].Parameter := -NewProgram[NewProgramIndex - 1].Parameter; 632 if NewProgram[NewProgramIndex - 1].Command = cmPointerInc then 633 NewProgram[NewProgramIndex - 1].Command := cmPointerDec 634 else NewProgram[NewProgramIndex - 1].Command := cmPointerInc; 635 end; 636 if NewProgram[NewProgramIndex - 1].Parameter = 0 then Dec(NewProgramIndex); 637 Dec(NewProgramIndex); 638 end else begin 639 NewProgram[NewProgramIndex].Command := cmPointerInc; 640 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 641 end; 642 end; 643 cmPointerDec: begin 644 if PreviousCommand in [cmPointerInc, cmPointerDec] then begin 645 if NewProgram[NewProgramIndex - 1].Command = cmPointerDec then 646 NewProgram[NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter + 647 FProgram[FProgramIndex].Parameter 648 else if NewProgram[NewProgramIndex - 1].Command = cmPointerInc then 649 NewProgram[NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter - 650 FProgram[FProgramIndex].Parameter; 651 // If value negative then change command 652 if NewProgram[NewProgramIndex - 1].Parameter < 0 then begin 653 NewProgram[NewProgramIndex - 1].Parameter := -NewProgram[NewProgramIndex - 1].Parameter; 654 if NewProgram[NewProgramIndex - 1].Command = cmPointerInc then 655 NewProgram[NewProgramIndex - 1].Command := cmPointerDec 656 else NewProgram[NewProgramIndex - 1].Command := cmPointerInc; 657 end; 658 if NewProgram[NewProgramIndex - 1].Parameter = 0 then Dec(NewProgramIndex); 659 Dec(NewProgramIndex); 660 end else begin 661 NewProgram[NewProgramIndex].Command := cmPointerDec; 662 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 663 end; 664 end; 665 cmInc: begin 666 if PreviousCommand in [cmInc, cmDec, cmSet] then begin 667 if NewProgram[NewProgramIndex - 1].Command in [cmInc, cmSet] then 668 NewProgram[NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter + 669 FProgram[FProgramIndex].Parameter 670 else if NewProgram[NewProgramIndex - 1].Command = cmDec then 671 NewProgram[NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter - 672 FProgram[FProgramIndex].Parameter; 673 // If value negative then change command 674 if (NewProgram[NewProgramIndex - 1].Parameter < 0) and (NewProgram[NewProgramIndex - 1].Command <> cmSet) then begin 675 NewProgram[NewProgramIndex - 1].Parameter := -NewProgram[NewProgramIndex - 1].Parameter; 676 if NewProgram[NewProgramIndex - 1].Command = cmInc then 677 NewProgram[NewProgramIndex - 1].Command := cmDec 678 else NewProgram[NewProgramIndex - 1].Command := cmInc; 679 end; 680 if NewProgram[NewProgramIndex - 1].Parameter = 0 then Dec(NewProgramIndex); 681 Dec(NewProgramIndex); 682 end else begin 683 NewProgram[NewProgramIndex].Command := cmInc; 684 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 685 end; 686 end; 687 cmDec: begin 688 if PreviousCommand in [cmInc, cmDec, cmSet] then begin 689 if NewProgram[NewProgramIndex - 1].Command = cmDec then 690 NewProgram[NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter + 691 FProgram[FProgramIndex].Parameter 692 else if NewProgram[NewProgramIndex - 1].Command in [cmInc, cmSet] then 693 NewProgram[NewProgramIndex - 1].Parameter := NewProgram[NewProgramIndex - 1].Parameter - 694 FProgram[FProgramIndex].Parameter; 695 // If value negative then change command 696 if (NewProgram[NewProgramIndex - 1].Parameter < 0) and (NewProgram[NewProgramIndex - 1].Command <> cmSet) then begin 697 NewProgram[NewProgramIndex - 1].Parameter := -NewProgram[NewProgramIndex - 1].Parameter; 698 if NewProgram[NewProgramIndex - 1].Command = cmInc then 699 NewProgram[NewProgramIndex - 1].Command := cmDec 700 else NewProgram[NewProgramIndex - 1].Command := cmInc; 701 end; 702 if NewProgram[NewProgramIndex - 1].Parameter = 0 then Dec(NewProgramIndex); 703 Dec(NewProgramIndex); 704 end else begin 705 NewProgram[NewProgramIndex].Command := cmDec; 706 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 707 end; 708 end; 709 cmSet: begin 710 if PreviousCommand in [cmInc, cmDec, cmSet] then begin 711 // Set overrides value of previous commands 712 Dec(NewProgramIndex); 713 NewProgram[NewProgramIndex].Command := cmSet; 714 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 715 end else begin 716 NewProgram[NewProgramIndex].Command := cmSet; 717 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 718 end; 719 end; 720 cmLoopStart: begin 721 if CheckClear then begin 722 NewProgram[NewProgramIndex].Command := cmSet; 723 NewProgram[NewProgramIndex].Parameter := 0; 724 Inc(FProgramIndex, 2); 725 end else begin 726 NewProgram[NewProgramIndex].Command := FProgram[FProgramIndex].Command; 727 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 728 end; 729 end; 730 else begin 731 NewProgram[NewProgramIndex].Command := FProgram[FProgramIndex].Command; 732 NewProgram[NewProgramIndex].Parameter := FProgram[FProgramIndex].Parameter; 733 end; 734 end; 735 PreviousCommand := FProgram[FProgramIndex].Command; 736 Inc(FProgramIndex); 737 Inc(NewProgramIndex); 738 end; 739 SetLength(NewProgram, NewProgramIndex); 740 741 // Replace old program by new program 742 SetLength(FProgram, Length(NewProgram)); 743 Move(NewProgram[0], FProgram[0], SizeOf(TMachineOperation) * Length(NewProgram)); 744 end; 745 746 procedure TTarget.OptimizeZeroInitMemory; 747 begin 748 // Here optimization related to assumption that initial memory is filled with zeroes 749 // Then code for constants preparation can be translated to cmSet commands 750 // To eliminate also loops for building constants code need to be somehow interpretted partialy 538 751 end; 539 752 … … 549 762 case FSourceCode[I] of 550 763 '+': begin 551 FProgram[FProgramIndex] := cmInc; 764 FProgram[FProgramIndex].Command := cmInc; 765 FProgram[FProgramIndex].Parameter := 1; 552 766 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 553 767 end; 554 768 '-': begin 555 FProgram[FProgramIndex] := cmDec; 769 FProgram[FProgramIndex].Command := cmDec; 770 FProgram[FProgramIndex].Parameter := 1; 556 771 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 557 772 end; 558 773 '>': begin 559 FProgram[FProgramIndex] := cmPointerInc; 774 FProgram[FProgramIndex].Command := cmPointerInc; 775 FProgram[FProgramIndex].Parameter := 1; 560 776 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 561 777 end; 562 778 '<': begin 563 FProgram[FProgramIndex] := cmPointerDec; 779 FProgram[FProgramIndex].Command := cmPointerDec; 780 FProgram[FProgramIndex].Parameter := 1; 564 781 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 565 782 end; 566 783 ',': begin 567 FProgram[FProgramIndex] := cmInput; 784 FProgram[FProgramIndex].Command := cmInput; 785 FProgram[FProgramIndex].Parameter := 0; 568 786 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 569 787 end; 570 788 '.': begin 571 FProgram[FProgramIndex] := cmOutput; 789 FProgram[FProgramIndex].Command := cmOutput; 790 FProgram[FProgramIndex].Parameter := 0; 572 791 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 573 792 end; 574 793 '[': begin 575 FProgram[FProgramIndex] := cmLoopStart; 794 FProgram[FProgramIndex].Command := cmLoopStart; 795 FProgram[FProgramIndex].Parameter := 0; 576 796 DebugSteps.AddStep(I - 1, FProgramIndex, soStepIn); 577 797 end; 578 798 ']': begin 579 FProgram[FProgramIndex] := cmLoopEnd; 799 FProgram[FProgramIndex].Command := cmLoopEnd; 800 FProgram[FProgramIndex].Parameter := 0; 580 801 DebugSteps.AddStep(I - 1, FProgramIndex, soStepOut); 581 802 end … … 594 815 function TTarget.CheckClear: Boolean; 595 816 begin 596 Result := (FProgram[FProgramIndex] = cmLoopStart) and (Length(FProgram) >= FProgramIndex + 2) and 597 (FProgram[FProgramIndex + 1] = cmDec) and (FProgram[FProgramIndex + 2] = cmLoopEnd); 817 Result := (FProgram[FProgramIndex].Command = cmLoopStart) and (Length(FProgram) >= FProgramIndex + 2) and 818 (((FProgram[FProgramIndex + 1].Command = cmDec) and (FProgram[FProgramIndex + 1].Parameter = 1)) or 819 ((FProgram[FProgramIndex + 1].Command = cmInc) and (FProgram[FProgramIndex + 1].Parameter = -1))) 820 and (FProgram[FProgramIndex + 2].Command = cmLoopEnd); 598 821 end; 599 822
Note:
See TracChangeset
for help on using the changeset viewer.