home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 February / Chip_2000-02_cd.bin / zkuste / Delphi / navody / tt / objvm.exe / UNITS / Decomps.pas < prev    next >
Pascal/Delphi Source File  |  1998-06-26  |  6KB  |  157 lines

  1. unit Decomps;
  2.  
  3. interface
  4. uses
  5.      Code,OpCodes,SysUtils,ObjList;
  6. type
  7.   {TOpDecomp provides interface to decompile operation}
  8.      TOpDecomp=class
  9.      public
  10.              {Code of operation}
  11.         OpCode:TOpCode;
  12.              {AsString returns string representation of op}
  13.              {in given code. C is code, Pos is position of}
  14.              {operation being decompiling}
  15.         function AsString(c:TCode;Pos:integer):string;virtual;abstract;
  16.              {Length returns length of current operation in bytes.}
  17.              {Meanings of parameters are same as in AsString function}
  18.         function Length(c:TCode;Pos:integer):integer;virtual;abstract;
  19.      end;
  20. type TOpCodeDecomp=class(TOpDecomp)
  21.         Name:string;
  22.         constructor Create(anOpCode:TOpCode;const aName:string);
  23.         function AsString(c:TCode;Pos:integer):string;override;
  24.         function Length(c:TCode;Pos:integer):integer;override;
  25.      end;
  26. type TIntDecomp=class(TOpCodeDecomp)
  27.         function AsString(c:TCode;Pos:integer):string;override;
  28.         function Length(c:TCode;Pos:integer):integer;override;
  29.      end;
  30.  
  31. type TStrDecomp=class(TOpCodeDecomp)
  32.         function AsString(c:TCode;Pos:integer):string;override;
  33.         function Length(c:TCode;Pos:integer):integer;override;
  34.      end;
  35.  
  36. type TFloatDecomp=class(TOpCodeDecomp)
  37.         function AsString(c:TCode;Pos:integer):string;override;
  38.         function Length(c:TCode;Pos:integer):integer;override;
  39.      end;
  40.  
  41. type TBoolDecomp=class(TOpCodeDecomp)
  42.         function AsString(c:TCode;Pos:integer):string;override;
  43.         function Length(c:TCode;Pos:integer):integer;override;
  44.      end;
  45. procedure Register(o:TObjList);
  46. implementation
  47. constructor TOpCodeDecomp.Create;
  48.             begin
  49.               Inherited Create;
  50.               Name:=aName;
  51.               OpCode:=anOpCode;
  52.             end;
  53. function    TOpCodeDecomp.AsString;
  54.             begin
  55.               Result:=Name;
  56.             end;
  57. function    TOpCodeDecomp.Length;
  58.             begin
  59.               Result:=SizeOf(TOpCode);
  60.             end;
  61.  
  62. function    TIntDecomp.AsString;
  63.             begin
  64.               Result:=Inherited AsString(c,Pos)+' '+
  65.                   IntToStr(c.Int[Pos+inherited Length(c,Pos)]);
  66.             end;
  67. function    TIntDecomp.Length;
  68.             begin
  69.               Result:=Inherited Length(c,Pos)+SizeOf(Integer);
  70.             end;
  71.  
  72. function    TStrDecomp.AsString;
  73.             begin
  74.               Result:=Inherited AsString(c,Pos)+''''+
  75.                   c.Str[Pos+inherited Length(c,Pos)]+'''';
  76.             end;
  77. function    TStrDecomp.Length;
  78.             begin
  79.               Result:=Inherited Length(c,Pos)+System.Length(c.Str[Pos+inherited Length(c,Pos)])+1;
  80.             end;
  81.  
  82. function    TFloatDecomp.AsString;
  83.             begin
  84.               Result:=Inherited AsString(c,Pos)+' '+
  85.                   FloatToStr(c.Num[Pos+inherited Length(c,Pos)]);
  86.             end;
  87. function    TFloatDecomp.Length;
  88.             begin
  89.               Result:=Inherited Length(c,Pos)+SizeOf(Extended);
  90.             end;
  91.  
  92. function    TBoolDecomp.AsString;
  93.             begin
  94.               Result:=Inherited AsString(c,Pos);
  95.               if c.Int[Pos+inherited Length(c,Pos)]=0 then
  96.                 Result:=Result+' FALSE'
  97.               else
  98.                 Result:=Result+' TRUE';
  99.             end;
  100. function    TBoolDecomp.Length;
  101.             begin
  102.               Result:=Inherited Length(c,Pos)+SizeOf(Integer);
  103.             end;
  104.  
  105. procedure Register(o:TObjList);
  106.           begin
  107.             With o do
  108.             begin
  109.             { Arithmetic operations    }
  110.             Add(TOpCodeDecomp.Create(ocAdd,'+'));
  111.             Add(TOpCodeDecomp.Create(ocSub,'-'));
  112.             Add(TOpCodeDecomp.Create(ocMul,'*'));
  113.             Add(TOpCodeDecomp.Create(ocDiv,'/'));
  114.             Add(TOpCodeDecomp.Create(ocNegate,'NEGATE'));
  115.             { Boolean operations       }
  116.             Add(TOpCodeDecomp.Create(ocAnd,'AND'));
  117.             Add(TOpCodeDecomp.Create(ocOr,'OR'));
  118.             Add(TOpCodeDecomp.Create(ocNot,'NOT'));
  119.             {Comparison ops}
  120.             Add(TOpCodeDecomp.Create(ocEqu,'='));
  121.             Add(TOpCodeDecomp.Create(ocNE,'<>'));
  122.             Add(TOpCodeDecomp.Create(ocG,'>'));
  123.             Add(TOpCodeDecomp.Create(ocL,'<'));
  124.             Add(TOpCodeDecomp.Create(ocLE,'<='));
  125.             Add(TOpCodeDecomp.Create(ocGE,'>='));
  126.             {Value operations          }
  127.             Add(TOpCodeDecomp.Create(ocGet,'@'));
  128.             Add(TOpCodeDecomp.Create(ocSet,'!'));
  129.             Add(TOpCodeDecomp.Create(ocExec,'EXEC'));
  130.             Add(TOpCodeDecomp.Create(ocEval,'EVAL'));
  131.             Add(TOpCodeDecomp.Create(ocGetElem,'[]'));
  132.             Add(TOpCodeDecomp.Create(ocRoot,'ROOT'));
  133.             {Constant operations       }
  134.             Add(TStrDecomp.Create(ocStr,'STR#'));
  135.             Add(TFloatDecomp.Create(ocFloat,'F#'));
  136.             Add(TIntDecomp.Create(ocInt,'#'));
  137.             Add(TBoolDecomp.Create(ocBool,'B#'));
  138.             {Execution flow operations }
  139.             Add(TOpCodeDecomp.Create(ocNop,'NOP'));
  140.             Add(TOpCodeDecomp.Create(ocHalt,';'));
  141.             Add(TIntDecomp.Create(ocJZ,'JZ'));
  142.             Add(TIntDecomp.Create(ocJNZ,'JNZ'));
  143.             Add(TIntDecomp.Create(ocJMP,'JMP'));
  144.               {Jump if top of Return stack is Zero}
  145.             Add(TIntDecomp.Create(ocJRZ,'JRZ'));
  146.               {DeCRement top of return stack}
  147.             Add(TOpCodeDecomp.Create(ocDCRR,'DCRR'));
  148.               {Moves value to return stack}
  149.             Add(TOpCodeDecomp.Create(ocToR,'>R'));
  150.               {Moves value from return stack to data stack}
  151.             Add(TOpCodeDecomp.Create(ocFromR,'R>'));
  152.               {Drops value from reaturn stack}
  153.             Add(TOpCodeDecomp.Create(ocRDROP,'RDROP'));
  154.             end;
  155.           end;
  156. end.
  157.