Ignore:
Timestamp:
Apr 9, 2009, 9:53:40 AM (16 years ago)
Author:
george
Message:
  • Upraveno: Rozdělení původního kódu do více tříd.
Location:
branches/DelphiToC
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/DelphiToC

    • Property svn:ignore set to
      *.dsk
      *.exe
      *.res
      *.~dsk
      *.dcu
  • branches/DelphiToC/UAssemblerSource.pas

    r12 r13  
    22
    33interface
     4
     5uses
     6  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
     7  Dialogs, StdCtrls, UPascalSource, UCodeProducer;
    48
    59type
     
    1115  end;
    1216
     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
    1338implementation
     39
     40
     41{ TAssemblerLine }
     42
     43function TAssemblerLine.AsString: string;
     44begin
     45  if LabelName = '' then LabelName := #9 else
     46    LabelName := LabelName + ':'#9;
     47  if Operand2 <> '' then Operand1 := Operand1 + ', ';
     48
     49  Result := LabelName + Instruction + ' ' + Operand1 + Operand2;
     50end;
     51
     52{ TAssemblerProducer }
     53
     54procedure TAssemblerProducer.AddInstruction(LabelName, Instruction, Operand1,
     55  Operand2: string);
     56var
     57  NewLine: TAssemblerLine;
     58begin
     59  NewLine := TAssemblerLine.Create;
     60  AssemblyCode.Add(NewLine);
     61  NewLine.LabelName := LabelName;
     62  NewLine.Instruction := Instruction;
     63  NewLine.Operand1 := Operand1;
     64  NewLine.Operand2 := Operand2;
     65end;
     66
    1467
    1568(*
     
    4396*)
    4497
     98destructor TAssemblerProducer.Destroy;
     99var
     100  I: Integer;
     101begin
     102  for I := 0 to AssemblyCode.Count - 1 do
     103    TAssemblerLine(AssemblyCode[I]).Free;
     104  AssemblyCode.Free;
     105  inherited;
     106end;
     107
     108procedure TAssemblerProducer.GenerateCommonBlock(CommonBlock: TCommonBlock; LabelPrefix: string);
     109var
     110  I: Integer;
     111  LabelName: string;
     112begin
     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;
     134end;
     135
     136procedure TAssemblerProducer.GenerateExpression(Expression: TExpression; LabelPrefix: string);
     137var
     138  I: Integer;
     139begin
     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;
     168end;
     169
     170procedure TAssemblerProducer.GenerateProgram(ProgramBlock: TProgram);
     171var
     172  I: Integer;
     173begin
     174  with ProgramBlock do
     175  for I := 0 to Modules.Count - 1 do
     176    GenerateCommonBlock(TModule(Modules[I]), '');
     177end;
     178
     179
     180
     181
    45182end.
Note: See TracChangeset for help on using the changeset viewer.