2
0

rasm.pas 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. {
  2. Copyright (c) 1998-2003 by Peter Vreman, Florian Klaempfl and Carl Eric Codere
  3. Basic stuff for assembler readers
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit rasm;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. symsym,
  23. rabase,
  24. aasmbase,
  25. aasmdata,
  26. cpubase,
  27. cgbase;
  28. type
  29. tasmreader = class(tbaseasmreader)
  30. firsttoken : boolean;
  31. _asmsorted : boolean;
  32. curlist : TAsmList;
  33. c : char;
  34. actasmpattern : string;
  35. actopcode : tasmop;
  36. actasmregister : tregister;
  37. actcondition : tasmcond;
  38. iasmops : TFPHashList;
  39. locallabels : TFPHashObjectList;
  40. constructor create;override;
  41. destructor destroy;override;
  42. function createlocallabel(const s: string; var hl: tasmlabel; emit: boolean): boolean;
  43. procedure checklocallabels;
  44. protected
  45. { allow subscripting a local if it is a:
  46. 1) pointer to a records/object, or a class instance pointer, passed in a register to a pure assembler routine
  47. 2) record located on the stack (passed as parameter, or local variable)
  48. }
  49. procedure checklocalsubscript(sym: tabstractnormalvarsym); virtual;
  50. end;
  51. implementation
  52. uses
  53. verbose,
  54. procinfo,
  55. symconst,symdef,
  56. paramgr;
  57. type
  58. TLocalLabel = class(TFPHashObject)
  59. emitted: boolean;
  60. lab: tasmlabel;
  61. function Gettasmlabel: tasmlabel;
  62. end;
  63. function TLocalLabel.Gettasmlabel:tasmlabel;
  64. begin
  65. if not assigned(lab) then
  66. begin
  67. current_asmdata.getjumplabel(lab);
  68. { this label is forced to be used so it's always written }
  69. lab.increfs;
  70. end;
  71. result:=lab;
  72. end;
  73. constructor tasmreader.create;
  74. begin
  75. inherited create;
  76. firsttoken:=true;
  77. locallabels:=TFPHashObjectList.create;
  78. end;
  79. destructor tasmreader.destroy;
  80. begin
  81. locallabels.Free;
  82. iasmops.Free;
  83. inherited destroy;
  84. end;
  85. function tasmreader.createlocallabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  86. var
  87. lab: TLocalLabel;
  88. begin
  89. result:=true;
  90. { Check if it already is defined }
  91. lab:=TLocalLabel(locallabels.Find(s));
  92. if not assigned(lab) then
  93. lab:=TLocalLabel.Create(locallabels,s);
  94. { set emitted flag and check for dup syms }
  95. if emit then
  96. begin
  97. if lab.Emitted then
  98. begin
  99. Message1(asmr_e_dup_local_sym,lab.Name);
  100. result:=false;
  101. end;
  102. lab.Emitted:=true;
  103. end;
  104. hl:=lab.Gettasmlabel;
  105. end;
  106. procedure tasmreader.checklocallabels;
  107. var
  108. i: longint;
  109. lab: TLocalLabel;
  110. begin
  111. for i:=0 to locallabels.Count-1 do
  112. begin
  113. lab:=TLocalLabel(locallabels[i]);
  114. if not lab.emitted then
  115. Message1(asmr_e_unknown_label_identifier,lab.name);
  116. end;
  117. locallabels.Clear;
  118. end;
  119. procedure tasmreader.checklocalsubscript(sym: tabstractnormalvarsym);
  120. var
  121. isimplicitpointer: boolean;
  122. begin
  123. isimplicitpointer:=
  124. (sym.typ=paravarsym) and
  125. (is_implicit_pointer_object_type(sym.vardef) or
  126. paramanager.push_addr_param(sym.varspez,sym.vardef,current_procinfo.procdef.proccalloption));
  127. { sym.initiallloc/localloc is not yet initialised here }
  128. { pointer parameter to aggregate passed in register to pure assembler routine }
  129. if (po_assembler in current_procinfo.procdef.procoptions) and
  130. (sym.typ=paravarsym) and
  131. (tparavarsym(sym).paraloc[calleeside].location^.loc=LOC_REGISTER) and
  132. { ... however this is only possible if the pointer takes only one register location }
  133. not assigned(tparavarsym(sym).paraloc[calleeside].location^.Next) and
  134. isimplicitpointer then
  135. exit;
  136. { aggregate parameter passed on the stack to a pure assembler routine }
  137. if (po_assembler in current_procinfo.procdef.procoptions) and
  138. (sym.typ=paravarsym) and
  139. { sym.localloc is not yet initialised here for pure assembler routines }
  140. (tparavarsym(sym).paraloc[calleeside].location^.loc in [LOC_REFERENCE,LOC_CREFERENCE]) and
  141. not isimplicitpointer then
  142. exit;
  143. { aggregate parameter located on the stack for a non-assembler routine
  144. (locals accessed from assembler code are never kept in registers) }
  145. if not(po_assembler in current_procinfo.procdef.procoptions) and
  146. (sym.typ=paravarsym) and
  147. not isimplicitpointer then
  148. exit;
  149. { local aggregate located on the stack (locals accessed from assembler
  150. code are never kept in registers) }
  151. if ((sym.typ=localvarsym) or
  152. { even if a parameter is passed by reference, it will be copied to
  153. a local if it's a value parameter to a non assembler routines }
  154. (not(po_assembler in current_procinfo.procdef.procoptions) and
  155. (sym.typ=paravarsym) and
  156. (sym.varspez=vs_value))) and
  157. not is_implicit_pointer_object_type(sym.vardef) then
  158. exit;
  159. Message(asmr_e_cannot_access_field_directly_for_parameters);
  160. end;
  161. end.