ra386dir.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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,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. sym : psym;
  53. code : TAAsmoutput;
  54. i,l : longint;
  55. procedure writeasmline;
  56. var
  57. i : longint;
  58. begin
  59. i:=length(s);
  60. while (i>0) and (s[i] in [' ',#9]) do
  61. dec(i);
  62. s[0]:=chr(i);
  63. if s<>'' then
  64. code.concat(Tai_direct.Create(strpnew(s)));
  65. { consider it set function set if the offset was loaded }
  66. if assigned(procinfo^.returntype.def) and
  67. (pos(retstr,upper(s))>0) then
  68. procinfo^.funcret_state:=vs_assigned;
  69. s:='';
  70. end;
  71. begin
  72. ende:=false;
  73. s:='';
  74. if assigned(procinfo^.returntype.def) and
  75. is_fpu(procinfo^.returntype.def) then
  76. procinfo^.funcret_state:=vs_assigned;
  77. if assigned(procinfo^.returntype.def) and
  78. (procinfo^.returntype.def<>pdef(voiddef)) then
  79. retstr:=upper(tostr(procinfo^.return_offset)+'('+att_reg2str[procinfo^.framepointer]+')')
  80. else
  81. retstr:='';
  82. c:=current_scanner^.asmgetchar;
  83. code:=TAAsmoutput.Create;
  84. while not(ende) do
  85. begin
  86. { wrong placement
  87. current_scanner^.gettokenpos; }
  88. case c of
  89. 'A'..'Z','a'..'z','_' : begin
  90. current_scanner^.gettokenpos;
  91. i:=0;
  92. hs:='';
  93. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  94. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  95. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  96. or (c='_') do
  97. begin
  98. inc(i);
  99. hs[i]:=c;
  100. c:=current_scanner^.asmgetchar;
  101. end;
  102. hs[0]:=chr(i);
  103. if upper(hs)='END' then
  104. ende:=true
  105. else
  106. begin
  107. if c=':' then
  108. begin
  109. getsym(upper(hs),false);
  110. if srsym<>nil then
  111. if (srsym^.typ = labelsym) then
  112. Begin
  113. hs:=plabelsym(srsym)^.lab^.name;
  114. plabelsym(srsym)^.lab^.is_set:=true;
  115. end
  116. else
  117. Message(asmr_w_using_defined_as_local);
  118. end
  119. else if upper(hs)='FWAIT' then
  120. FwaitWarning
  121. else
  122. { access to local variables }
  123. if assigned(aktprocsym) then
  124. begin
  125. { is the last written character an special }
  126. { char ? }
  127. if (s[length(s)]='%') and
  128. ret_in_acc(procinfo^.returntype.def) and
  129. ((pos('AX',upper(hs))>0) or
  130. (pos('AL',upper(hs))>0)) then
  131. procinfo^.funcret_state:=vs_assigned;
  132. if (s[length(s)]<>'%') and
  133. (s[length(s)]<>'$') and
  134. ((s[length(s)]<>'0') or (hs[1]<>'x')) then
  135. begin
  136. if assigned(aktprocsym^.definition^.localst) and
  137. (lexlevel >= normal_function_level) then
  138. sym:=psym(aktprocsym^.definition^.localst^.search(upper(hs)))
  139. else
  140. sym:=nil;
  141. if assigned(sym) then
  142. begin
  143. if (sym^.typ = labelsym) then
  144. Begin
  145. hs:=plabelsym(sym)^.lab^.name;
  146. end
  147. else if sym^.typ=varsym then
  148. begin
  149. {variables set are after a comma }
  150. {like in movl %eax,I }
  151. if pos(',',s) > 0 then
  152. pvarsym(sym)^.varstate:=vs_used
  153. else
  154. if (pos('MOV',upper(s)) > 0) and (pvarsym(sym)^.varstate=vs_declared) then
  155. Message1(sym_n_uninitialized_local_variable,hs);
  156. if (vo_is_external in pvarsym(sym)^.varoptions) then
  157. hs:=pvarsym(sym)^.mangledname
  158. else
  159. hs:='-'+tostr(pvarsym(sym)^.address)+
  160. '('+att_reg2str[procinfo^.framepointer]+')';
  161. end
  162. else
  163. { call to local function }
  164. if (sym^.typ=procsym) and ((pos('CALL',upper(s))>0) or
  165. (pos('LEA',upper(s))>0)) then
  166. begin
  167. hs:=pprocsym(sym)^.definition^.mangledname;
  168. end;
  169. end
  170. else
  171. begin
  172. if assigned(aktprocsym^.definition^.parast) then
  173. sym:=psym(aktprocsym^.definition^.parast^.search(upper(hs)))
  174. else
  175. sym:=nil;
  176. if assigned(sym) then
  177. begin
  178. if sym^.typ=varsym then
  179. begin
  180. l:=pvarsym(sym)^.address;
  181. { set offset }
  182. inc(l,aktprocsym^.definition^.parast^.address_fixup);
  183. hs:=tostr(l)+'('+att_reg2str[procinfo^.framepointer]+')';
  184. if pos(',',s) > 0 then
  185. pvarsym(sym)^.varstate:=vs_used;
  186. end;
  187. end
  188. { I added that but it creates a problem in line.ppi
  189. because there is a local label wbuffer and
  190. a static variable WBUFFER ...
  191. what would you decide, florian ?}
  192. else
  193. begin
  194. {$ifndef IGNOREGLOBALVAR}
  195. getsym(upper(hs),false);
  196. sym:=srsym;
  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.4 2000-12-25 00:07:34 peter
  275. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  276. tlinkedlist objects)
  277. Revision 1.3 2000/11/29 00:30:50 florian
  278. * unused units removed from uses clause
  279. * some changes for widestrings
  280. Revision 1.2 2000/10/31 22:02:57 peter
  281. * symtable splitted, no real code changes
  282. Revision 1.1 2000/10/15 09:47:43 peter
  283. * moved to i386/
  284. Revision 1.5 2000/10/14 10:14:52 peter
  285. * moehrendorf oct 2000 rewrite
  286. Revision 1.4 2000/09/24 15:06:26 peter
  287. * use defines.inc
  288. Revision 1.3 2000/08/27 16:11:52 peter
  289. * moved some util functions from globals,cobjects to cutils
  290. * splitted files into finput,fmodule
  291. Revision 1.2 2000/07/13 11:32:48 michael
  292. + removed logs
  293. }