Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Курсовая работа1 / CodeGenerator / UCcCodeGeneratorCommands

.pas
Скачиваний:
12
Добавлен:
01.05.2014
Размер:
6 Кб
Скачать
{
  C-like language compiler v0.1.0.485

  12 november 2007

  Copyright (C) 2006, 2007 Igor Krooshch
  rheo.ikro@gmail.com

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
}

unit UCcCodeGeneratorCommands;

interface

uses
  SysUtils, Classes;

type
  TCcCommandType = (
    ctPush, ctPop, ctTest, ctMov, ctJz, ctJmp, ctInc, ctDec, ctImul, ctIdiv,
    ctAdd, ctSub, ctNeg, ctAnd, ctOr, ctXor, ctNot, ctShr, ctShl, ctNop,
    ctCmp, ctSetE, ctSetNE, ctSetG, ctSetGE, ctSetL, ctSetLE, ctComment, ctCdq,
    ctRet, ctCall, ctLea, ctInvoke, ctSetA, ctSetB, ctSetAE, ctSetBE,

    ctFld, ctFild, ctFldz, ctFst, ctFstp, ctFist, ctFistp, ctFadd,
    ctFiadd, ctFsub, ctFisub, ctFmul, ctFimul, ctFdiv, ctFidiv,
    ctFcom, ctFicom, ctFicomp,
    ctFtst, ctSahf, ctFchs, ctFabs, ctFsqrt, ctFcomi);

  TCcSegmentType = (stCode, stData, stStack, stExt);

  TCcLabelType = (ltFunc, ltIf, ltWhile, ltFor, ltDoWhile);

  TCcCommand = class
  private
    FLab: string;
    FCommand: TCcCommandType;
    FComment: string;
  public
    constructor Create(ACommand: TCcCommandType; const AComment: string = '');

    procedure WriteOutput(A: TStringList); virtual;

    function GenerateComment: string;

    property Command: TCcCommandType read FCommand;
    property Lab: string read FLab write FLab;
    property Comment: string read FComment write FComment;
  end;


  TCcCommentCommand = class(TCcCommand)
  public
    constructor Create(const AText: string); reintroduce;
    procedure WriteOutput(A: TStringList); override;
  end;


  TCcOneOperandCommand = class(TCcCommand)
  private
    FOperand: string;
  public
    constructor Create(
      ACommand: TCcCommandType; const AOperand: string;
      const AComment: string = ''); reintroduce;
    procedure WriteOutput(A: TStringList); override;

    property Operand: string read FOperand;
  end;


  TCcTwoOperandsCommand = class(TCcCommand)
  private
    FLeftOperand: string;
    FRightOperand: string;
  public
    constructor Create(
      ACommand: TCcCommandType;
      const ALeft, ARight: string; const AComment: string = ''); reintroduce;
    procedure WriteOutput(A: TStringList); override;

    property Left: string read FLeftOperand;
    property Right: string read FRightOperand;
  end;


  TCcHeader = class(TCcCommand)
  private
    FText: string;
  public
    constructor Create(
      const AText: string; const AComment: string = ''); reintroduce;

    procedure WriteOutput(A: TStringList); override;

    property Text: string read FText;
  end;


  TCcVarDeclaration = class(TCcHeader)
  private
    FGlobal: boolean;
  public
    constructor Create(
      AText: string; AGlobal: boolean;
      const AComment: string = ''); reintroduce;

    property Global: boolean read FGlobal;
  end;

const
  SEGMENT_TYPE: array[TCcSegmentType] of string = ('', 'ds', 'ss', 'es');

  
implementation

uses
  Types, TypInfo;

{ TCcCommand }

constructor TCcCommand.Create(ACommand: TCcCommandType; const AComment: string);
begin
  FCommand := ACommand;
  FComment := AComment;
end;


function TCcCommand.GenerateComment: string;
begin
  Result := '';
  if FComment <> '' then
    Result := #9 + '; ' + FComment;
end;


procedure TCcCommand.WriteOutput(A: TStringList);
var
  s: string;
begin
  s := GetEnumName(TypeInfo(TCcCommandType), Ord(Command));
  Delete(s, 1, 2);
  A.Add('  ' + s + GenerateComment);
end;


{ TCcOneOperandCommand }

constructor TCcOneOperandCommand.Create(
  ACommand: TCcCommandType; const AOperand: string; const AComment: string);
begin
  inherited Create(ACommand, AComment);
  FOperand := AOperand;
end;


procedure TCcOneOperandCommand.WriteOutput(A: TStringList);
var
  s: string;
begin
  s := GetEnumName(TypeInfo(TCcCommandType), Ord(Command));
  Delete(s, 1, 2);
  A.Add('  ' + Format('%s %s', [s, Operand]) + GenerateComment);
end;


{ TCcTwoOperandsCommand }

constructor TCcTwoOperandsCommand.Create(
  ACommand: TCcCommandType; const ALeft, ARight: string;
  const AComment: string);
begin
  inherited Create(ACommand, AComment);
  FLeftOperand := ALeft;
  FRightOperand := ARight;
end;


procedure TCcTwoOperandsCommand.WriteOutput(A: TStringList);
var
  s: string;
begin
  s := GetEnumName(TypeInfo(TCcCommandType), Ord(Command));
  Delete(s, 1, 2);
  A.Add('  ' + Format('%s %s, %s', [s, Left, Right]) + GenerateComment);
end;


{ TCcCommentCommand }

constructor TCcCommentCommand.Create(const AText: string);
begin
  inherited Create(ctComment, AText);
end;


procedure TCcCommentCommand.WriteOutput(A: TStringList);
begin
  A.Add('; ' + Comment);
end;


{ TCcVarDeclaration }

constructor TCcVarDeclaration.Create(
  AText: string; AGlobal: boolean; const AComment: string);
begin
  inherited Create(AText, AComment);
  FGlobal := AGlobal;
end;


{ TCcHeader }

constructor TCcHeader.Create(const AText: string; const AComment: string);
begin
  FText := AText;
end;


procedure TCcHeader.WriteOutput(A: TStringList);
begin
  A.Add(FText + GenerateComment);
end;


end. 
Соседние файлы в папке CodeGenerator