ra386dir.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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,symsym,symtable,types,
  35. { pass 1 }
  36. nbas,
  37. { parser }
  38. scanner,
  39. ra386,
  40. { codegen }
  41. cgbase
  42. ;
  43. function assemble : tnode;
  44. var
  45. retstr,s,hs : string;
  46. c : char;
  47. ende : boolean;
  48. srsym,sym : tsym;
  49. srsymtable : tsymtable;
  50. code : TAAsmoutput;
  51. i,l : longint;
  52. procedure writeasmline;
  53. var
  54. i : longint;
  55. begin
  56. i:=length(s);
  57. while (i>0) and (s[i] in [' ',#9]) do
  58. dec(i);
  59. s[0]:=chr(i);
  60. if s<>'' then
  61. code.concat(Tai_direct.Create(strpnew(s)));
  62. { consider it set function set if the offset was loaded }
  63. if assigned(aktprocsym.definition.funcretsym) and
  64. (pos(retstr,upper(s))>0) then
  65. tfuncretsym(aktprocsym.definition.funcretsym).funcretstate:=vs_assigned;
  66. s:='';
  67. end;
  68. begin
  69. ende:=false;
  70. s:='';
  71. if assigned(aktprocsym.definition.funcretsym) and
  72. is_fpu(aktprocsym.definition.rettype.def) then
  73. tfuncretsym(aktprocsym.definition.funcretsym).funcretstate:=vs_assigned;
  74. if (not is_void(aktprocsym.definition.rettype.def)) then
  75. retstr:=upper(tostr(procinfo^.return_offset)+'('+att_reg2str[procinfo^.framepointer]+')')
  76. else
  77. retstr:='';
  78. c:=current_scanner.asmgetchar;
  79. code:=TAAsmoutput.Create;
  80. while not(ende) do
  81. begin
  82. { wrong placement
  83. current_scanner.gettokenpos; }
  84. case c of
  85. 'A'..'Z','a'..'z','_' : begin
  86. current_scanner.gettokenpos;
  87. i:=0;
  88. hs:='';
  89. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  90. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  91. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  92. or (c='_') do
  93. begin
  94. inc(i);
  95. hs[i]:=c;
  96. c:=current_scanner.asmgetchar;
  97. end;
  98. hs[0]:=chr(i);
  99. if upper(hs)='END' then
  100. ende:=true
  101. else
  102. begin
  103. if c=':' then
  104. begin
  105. searchsym(upper(hs),srsym,srsymtable);
  106. if srsym<>nil then
  107. if (srsym.typ = labelsym) then
  108. Begin
  109. hs:=tlabelsym(srsym).lab.name;
  110. tlabelsym(srsym).lab.is_set:=true;
  111. end
  112. else
  113. Message(asmr_w_using_defined_as_local);
  114. end
  115. else if upper(hs)='FWAIT' then
  116. FwaitWarning
  117. else
  118. { access to local variables }
  119. if assigned(aktprocsym) then
  120. begin
  121. { is the last written character an special }
  122. { char ? }
  123. if (s[length(s)]='%') and
  124. ret_in_acc(aktprocsym.definition.rettype.def) and
  125. ((pos('AX',upper(hs))>0) or
  126. (pos('AL',upper(hs))>0)) then
  127. tfuncretsym(aktprocsym.definition.funcretsym).funcretstate:=vs_assigned;
  128. if (s[length(s)]<>'%') and
  129. (s[length(s)]<>'$') and
  130. ((s[length(s)]<>'0') or (hs[1]<>'x')) then
  131. begin
  132. if assigned(aktprocsym.definition.localst) and
  133. (lexlevel >= normal_function_level) then
  134. sym:=tsym(aktprocsym.definition.localst.search(upper(hs)))
  135. else
  136. sym:=nil;
  137. if assigned(sym) then
  138. begin
  139. if (sym.typ = labelsym) then
  140. Begin
  141. hs:=tlabelsym(sym).lab.name;
  142. end
  143. else if sym.typ=varsym then
  144. begin
  145. {variables set are after a comma }
  146. {like in movl %eax,I }
  147. if pos(',',s) > 0 then
  148. tvarsym(sym).varstate:=vs_used
  149. else
  150. if (pos('MOV',upper(s)) > 0) and (tvarsym(sym).varstate=vs_declared) then
  151. Message1(sym_n_uninitialized_local_variable,hs);
  152. if (vo_is_external in tvarsym(sym).varoptions) then
  153. hs:=tvarsym(sym).mangledname
  154. else
  155. hs:='-'+tostr(tvarsym(sym).address)+
  156. '('+att_reg2str[procinfo^.framepointer]+')';
  157. end
  158. else
  159. { call to local function }
  160. if (sym.typ=procsym) and ((pos('CALL',upper(s))>0) or
  161. (pos('LEA',upper(s))>0)) then
  162. begin
  163. hs:=tprocsym(sym).definition.mangledname;
  164. end;
  165. end
  166. else
  167. begin
  168. if assigned(aktprocsym.definition.parast) then
  169. sym:=tsym(aktprocsym.definition.parast.search(upper(hs)))
  170. else
  171. sym:=nil;
  172. if assigned(sym) then
  173. begin
  174. if sym.typ=varsym then
  175. begin
  176. l:=tvarsym(sym).address;
  177. { set offset }
  178. inc(l,aktprocsym.definition.parast.address_fixup);
  179. hs:=tostr(l)+'('+att_reg2str[procinfo^.framepointer]+')';
  180. if pos(',',s) > 0 then
  181. tvarsym(sym).varstate:=vs_used;
  182. end;
  183. end
  184. { I added that but it creates a problem in line.ppi
  185. because there is a local label wbuffer and
  186. a static variable WBUFFER ...
  187. what would you decide, florian ?}
  188. else
  189. begin
  190. {$ifndef IGNOREGLOBALVAR}
  191. searchsym(upper(hs),sym,srsymtable);
  192. if assigned(sym) and (sym.owner.symtabletype in [globalsymtable,staticsymtable]) then
  193. begin
  194. if (sym.typ = varsym) or (sym.typ = typedconstsym) then
  195. begin
  196. Message2(asmr_h_direct_global_to_mangled,hs,sym.mangledname);
  197. hs:=sym.mangledname;
  198. if sym.typ=varsym then
  199. inc(tvarsym(sym).refs);
  200. end;
  201. { procs can be called or the address can be loaded }
  202. if (sym.typ=procsym) and
  203. ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  204. begin
  205. if assigned(tprocsym(sym).definition.nextoverloaded) then
  206. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  207. Message2(asmr_h_direct_global_to_mangled,hs,sym.mangledname);
  208. hs:=sym.mangledname;
  209. end;
  210. end
  211. else
  212. {$endif TESTGLOBALVAR}
  213. if upper(hs)='__SELF' then
  214. begin
  215. if assigned(procinfo^._class) then
  216. hs:=tostr(procinfo^.selfpointer_offset)+
  217. '('+att_reg2str[procinfo^.framepointer]+')'
  218. else
  219. Message(asmr_e_cannot_use_SELF_outside_a_method);
  220. end
  221. else if upper(hs)='__RESULT' then
  222. begin
  223. if (not is_void(aktprocsym.definition.rettype.def)) then
  224. hs:=retstr
  225. else
  226. Message(asmr_e_void_function);
  227. end
  228. else if upper(hs)='__OLDEBP' then
  229. begin
  230. { complicate to check there }
  231. { we do it: }
  232. if lexlevel>normal_function_level then
  233. hs:=tostr(procinfo^.framepointer_offset)+
  234. '('+att_reg2str[procinfo^.framepointer]+')'
  235. else
  236. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  237. end;
  238. end;
  239. end;
  240. end;
  241. end;
  242. s:=s+hs;
  243. end;
  244. end;
  245. '{',';',#10,#13 : begin
  246. if pos(retstr,s) > 0 then
  247. tfuncretsym(aktprocsym.definition.funcretsym).funcretstate:=vs_assigned;
  248. writeasmline;
  249. c:=current_scanner.asmgetchar;
  250. end;
  251. #26 : Message(scan_f_end_of_file);
  252. else
  253. begin
  254. current_scanner.gettokenpos;
  255. inc(byte(s[0]));
  256. s[length(s)]:=c;
  257. c:=current_scanner.asmgetchar;
  258. end;
  259. end;
  260. end;
  261. writeasmline;
  262. assemble:=casmnode.create(code);
  263. end;
  264. {*****************************************************************************
  265. Initialize
  266. *****************************************************************************}
  267. const
  268. asmmode_i386_direct_info : tasmmodeinfo =
  269. (
  270. id : asmmode_i386_direct;
  271. idtxt : 'DIRECT'
  272. );
  273. initialization
  274. RegisterAsmMode(asmmode_i386_direct_info);
  275. end.
  276. {
  277. $Log$
  278. Revision 1.11 2001-08-26 13:37:02 florian
  279. * some cg reorganisation
  280. * some PPC updates
  281. Revision 1.10 2001/08/06 21:40:51 peter
  282. * funcret moved from tprocinfo to tprocdef
  283. Revision 1.9 2001/04/18 22:02:03 peter
  284. * registration of targets and assemblers
  285. Revision 1.8 2001/04/13 18:20:21 peter
  286. * scanner object to class
  287. Revision 1.7 2001/04/13 01:22:21 peter
  288. * symtable change to classes
  289. * range check generation and errors fixed, make cycle DEBUG=1 works
  290. * memory leaks fixed
  291. Revision 1.6 2001/04/02 21:20:40 peter
  292. * resulttype rewrite
  293. Revision 1.5 2001/03/11 22:58:52 peter
  294. * getsym redesign, removed the globals srsym,srsymtable
  295. Revision 1.4 2000/12/25 00:07:34 peter
  296. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  297. tlinkedlist objects)
  298. Revision 1.3 2000/11/29 00:30:50 florian
  299. * unused units removed from uses clause
  300. * some changes for widestrings
  301. Revision 1.2 2000/10/31 22:02:57 peter
  302. * symtable splitted, no real code changes
  303. Revision 1.1 2000/10/15 09:47:43 peter
  304. * moved to i386/
  305. Revision 1.5 2000/10/14 10:14:52 peter
  306. * moehrendorf oct 2000 rewrite
  307. Revision 1.4 2000/09/24 15:06:26 peter
  308. * use defines.inc
  309. Revision 1.3 2000/08/27 16:11:52 peter
  310. * moved some util functions from globals,cobjects to cutils
  311. * splitted files into finput,fmodule
  312. Revision 1.2 2000/07/13 11:32:48 michael
  313. + removed logs
  314. }