ra386dir.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Florian Klaempfl
  4. Reads inline assembler and writes the lines direct to the output
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. unit Ra386dir;
  19. {$i defines.inc}
  20. interface
  21. uses
  22. node;
  23. function assemble : tnode;
  24. implementation
  25. uses
  26. { common }
  27. cutils,
  28. { global }
  29. globals,verbose,
  30. systems,
  31. { aasm }
  32. cpubase,aasm,
  33. { symtable }
  34. symconst,symbase,symtype,symdef,symsym,symtable,types,
  35. { pass 1 }
  36. nbas,
  37. { parser }
  38. scanner,
  39. ra386,
  40. { codegen }
  41. {$ifdef newcg}
  42. cgbase
  43. {$else}
  44. hcodegen
  45. {$endif}
  46. ;
  47. function assemble : tnode;
  48. var
  49. retstr,s,hs : string;
  50. c : char;
  51. ende : boolean;
  52. srsym,sym : psym;
  53. srsymtable : psymtable;
  54. code : TAAsmoutput;
  55. i,l : longint;
  56. procedure writeasmline;
  57. var
  58. i : longint;
  59. begin
  60. i:=length(s);
  61. while (i>0) and (s[i] in [' ',#9]) do
  62. dec(i);
  63. s[0]:=chr(i);
  64. if s<>'' then
  65. code.concat(Tai_direct.Create(strpnew(s)));
  66. { consider it set function set if the offset was loaded }
  67. if assigned(procinfo^.returntype.def) and
  68. (pos(retstr,upper(s))>0) then
  69. procinfo^.funcret_state:=vs_assigned;
  70. s:='';
  71. end;
  72. begin
  73. ende:=false;
  74. s:='';
  75. if assigned(procinfo^.returntype.def) and
  76. is_fpu(procinfo^.returntype.def) then
  77. procinfo^.funcret_state:=vs_assigned;
  78. if assigned(procinfo^.returntype.def) and
  79. (procinfo^.returntype.def<>pdef(voiddef)) then
  80. retstr:=upper(tostr(procinfo^.return_offset)+'('+att_reg2str[procinfo^.framepointer]+')')
  81. else
  82. retstr:='';
  83. c:=current_scanner^.asmgetchar;
  84. code:=TAAsmoutput.Create;
  85. while not(ende) do
  86. begin
  87. { wrong placement
  88. current_scanner^.gettokenpos; }
  89. case c of
  90. 'A'..'Z','a'..'z','_' : begin
  91. current_scanner^.gettokenpos;
  92. i:=0;
  93. hs:='';
  94. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  95. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  96. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  97. or (c='_') do
  98. begin
  99. inc(i);
  100. hs[i]:=c;
  101. c:=current_scanner^.asmgetchar;
  102. end;
  103. hs[0]:=chr(i);
  104. if upper(hs)='END' then
  105. ende:=true
  106. else
  107. begin
  108. if c=':' then
  109. begin
  110. searchsym(upper(hs),srsym,srsymtable);
  111. if srsym<>nil then
  112. if (srsym^.typ = labelsym) then
  113. Begin
  114. hs:=plabelsym(srsym)^.lab^.name;
  115. plabelsym(srsym)^.lab^.is_set:=true;
  116. end
  117. else
  118. Message(asmr_w_using_defined_as_local);
  119. end
  120. else if upper(hs)='FWAIT' then
  121. FwaitWarning
  122. else
  123. { access to local variables }
  124. if assigned(aktprocsym) then
  125. begin
  126. { is the last written character an special }
  127. { char ? }
  128. if (s[length(s)]='%') and
  129. ret_in_acc(procinfo^.returntype.def) and
  130. ((pos('AX',upper(hs))>0) or
  131. (pos('AL',upper(hs))>0)) then
  132. procinfo^.funcret_state:=vs_assigned;
  133. if (s[length(s)]<>'%') and
  134. (s[length(s)]<>'$') and
  135. ((s[length(s)]<>'0') or (hs[1]<>'x')) then
  136. begin
  137. if assigned(aktprocsym^.definition^.localst) and
  138. (lexlevel >= normal_function_level) then
  139. sym:=psym(aktprocsym^.definition^.localst^.search(upper(hs)))
  140. else
  141. sym:=nil;
  142. if assigned(sym) then
  143. begin
  144. if (sym^.typ = labelsym) then
  145. Begin
  146. hs:=plabelsym(sym)^.lab^.name;
  147. end
  148. else if sym^.typ=varsym then
  149. begin
  150. {variables set are after a comma }
  151. {like in movl %eax,I }
  152. if pos(',',s) > 0 then
  153. pvarsym(sym)^.varstate:=vs_used
  154. else
  155. if (pos('MOV',upper(s)) > 0) and (pvarsym(sym)^.varstate=vs_declared) then
  156. Message1(sym_n_uninitialized_local_variable,hs);
  157. if (vo_is_external in pvarsym(sym)^.varoptions) then
  158. hs:=pvarsym(sym)^.mangledname
  159. else
  160. hs:='-'+tostr(pvarsym(sym)^.address)+
  161. '('+att_reg2str[procinfo^.framepointer]+')';
  162. end
  163. else
  164. { call to local function }
  165. if (sym^.typ=procsym) and ((pos('CALL',upper(s))>0) or
  166. (pos('LEA',upper(s))>0)) then
  167. begin
  168. hs:=pprocsym(sym)^.definition^.mangledname;
  169. end;
  170. end
  171. else
  172. begin
  173. if assigned(aktprocsym^.definition^.parast) then
  174. sym:=psym(aktprocsym^.definition^.parast^.search(upper(hs)))
  175. else
  176. sym:=nil;
  177. if assigned(sym) then
  178. begin
  179. if sym^.typ=varsym then
  180. begin
  181. l:=pvarsym(sym)^.address;
  182. { set offset }
  183. inc(l,aktprocsym^.definition^.parast^.address_fixup);
  184. hs:=tostr(l)+'('+att_reg2str[procinfo^.framepointer]+')';
  185. if pos(',',s) > 0 then
  186. pvarsym(sym)^.varstate:=vs_used;
  187. end;
  188. end
  189. { I added that but it creates a problem in line.ppi
  190. because there is a local label wbuffer and
  191. a static variable WBUFFER ...
  192. what would you decide, florian ?}
  193. else
  194. begin
  195. {$ifndef IGNOREGLOBALVAR}
  196. searchsym(upper(hs),sym,srsymtable);
  197. if assigned(sym) and (sym^.owner^.symtabletype in [unitsymtable,
  198. globalsymtable,staticsymtable]) then
  199. begin
  200. if (sym^.typ = varsym) or (sym^.typ = typedconstsym) then
  201. begin
  202. Message2(asmr_h_direct_global_to_mangled,hs,sym^.mangledname);
  203. hs:=sym^.mangledname;
  204. if sym^.typ=varsym then
  205. inc(pvarsym(sym)^.refs);
  206. end;
  207. { procs can be called or the address can be loaded }
  208. if (sym^.typ=procsym) and
  209. ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  210. begin
  211. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  212. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  213. Message2(asmr_h_direct_global_to_mangled,hs,sym^.mangledname);
  214. hs:=sym^.mangledname;
  215. end;
  216. end
  217. else
  218. {$endif TESTGLOBALVAR}
  219. if upper(hs)='__SELF' then
  220. begin
  221. if assigned(procinfo^._class) then
  222. hs:=tostr(procinfo^.selfpointer_offset)+
  223. '('+att_reg2str[procinfo^.framepointer]+')'
  224. else
  225. Message(asmr_e_cannot_use_SELF_outside_a_method);
  226. end
  227. else if upper(hs)='__RESULT' then
  228. begin
  229. if assigned(procinfo^.returntype.def) and
  230. (procinfo^.returntype.def<>pdef(voiddef)) then
  231. hs:=retstr
  232. else
  233. Message(asmr_e_void_function);
  234. end
  235. else if upper(hs)='__OLDEBP' then
  236. begin
  237. { complicate to check there }
  238. { we do it: }
  239. if lexlevel>normal_function_level then
  240. hs:=tostr(procinfo^.framepointer_offset)+
  241. '('+att_reg2str[procinfo^.framepointer]+')'
  242. else
  243. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  244. end;
  245. end;
  246. end;
  247. end;
  248. end;
  249. s:=s+hs;
  250. end;
  251. end;
  252. '{',';',#10,#13 : begin
  253. if pos(retstr,s) > 0 then
  254. procinfo^.funcret_state:=vs_assigned;
  255. writeasmline;
  256. c:=current_scanner^.asmgetchar;
  257. end;
  258. #26 : Message(scan_f_end_of_file);
  259. else
  260. begin
  261. current_scanner^.gettokenpos;
  262. inc(byte(s[0]));
  263. s[length(s)]:=c;
  264. c:=current_scanner^.asmgetchar;
  265. end;
  266. end;
  267. end;
  268. writeasmline;
  269. assemble:=casmnode.create(code);
  270. end;
  271. end.
  272. {
  273. $Log$
  274. Revision 1.5 2001-03-11 22:58:52 peter
  275. * getsym redesign, removed the globals srsym,srsymtable
  276. Revision 1.4 2000/12/25 00:07:34 peter
  277. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  278. tlinkedlist objects)
  279. Revision 1.3 2000/11/29 00:30:50 florian
  280. * unused units removed from uses clause
  281. * some changes for widestrings
  282. Revision 1.2 2000/10/31 22:02:57 peter
  283. * symtable splitted, no real code changes
  284. Revision 1.1 2000/10/15 09:47:43 peter
  285. * moved to i386/
  286. Revision 1.5 2000/10/14 10:14:52 peter
  287. * moehrendorf oct 2000 rewrite
  288. Revision 1.4 2000/09/24 15:06:26 peter
  289. * use defines.inc
  290. Revision 1.3 2000/08/27 16:11:52 peter
  291. * moved some util functions from globals,cobjects to cutils
  292. * splitted files into finput,fmodule
  293. Revision 1.2 2000/07/13 11:32:48 michael
  294. + removed logs
  295. }