ra386dir.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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(aktprocdef.funcretsym) and
  64. (pos(retstr,upper(s))>0) then
  65. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  66. s:='';
  67. end;
  68. begin
  69. ende:=false;
  70. s:='';
  71. if assigned(aktprocdef.funcretsym) and
  72. is_fpu(aktprocdef.rettype.def) then
  73. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  74. if (not is_void(aktprocdef.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(aktprocdef) then
  120. begin
  121. { is the last written character an special }
  122. { char ? }
  123. if (s[length(s)]='%') and
  124. ret_in_acc(aktprocdef.rettype.def) and
  125. ((pos('AX',upper(hs))>0) or
  126. (pos('AL',upper(hs))>0)) then
  127. tfuncretsym(aktprocdef.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(aktprocdef.localst) and
  133. (lexlevel >= normal_function_level) then
  134. sym:=tsym(aktprocdef.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).defs^.def.mangledname;
  164. end;
  165. end
  166. else
  167. begin
  168. if assigned(aktprocdef.parast) then
  169. sym:=tsym(aktprocdef.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,aktprocdef.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. searchsym(upper(hs),sym,srsymtable);
  191. if assigned(sym) and (sym.owner.symtabletype in [globalsymtable,staticsymtable]) then
  192. begin
  193. case sym.typ of
  194. varsym :
  195. begin
  196. Message2(asmr_h_direct_global_to_mangled,hs,tvarsym(sym).mangledname);
  197. hs:=tvarsym(sym).mangledname;
  198. inc(tvarsym(sym).refs);
  199. end;
  200. typedconstsym :
  201. begin
  202. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  203. hs:=ttypedconstsym(sym).mangledname;
  204. end;
  205. procsym :
  206. begin
  207. { procs can be called or the address can be loaded }
  208. if ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  209. begin
  210. if assigned(tprocsym(sym).defs^.def) then
  211. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  212. Message2(asmr_h_direct_global_to_mangled,hs,tprocsym(sym).defs^.def.mangledname);
  213. hs:=tprocsym(sym).defs^.def.mangledname;
  214. end;
  215. end;
  216. else
  217. Message(asmr_e_wrong_sym_type);
  218. end;
  219. end
  220. else if upper(hs)='__SELF' then
  221. begin
  222. if assigned(procinfo^._class) then
  223. hs:=tostr(procinfo^.selfpointer_offset)+
  224. '('+att_reg2str[procinfo^.framepointer]+')'
  225. else
  226. Message(asmr_e_cannot_use_SELF_outside_a_method);
  227. end
  228. else if upper(hs)='__RESULT' then
  229. begin
  230. if (not is_void(aktprocdef.rettype.def)) 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. tfuncretsym(aktprocdef.funcretsym).funcretstate:=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. {*****************************************************************************
  272. Initialize
  273. *****************************************************************************}
  274. const
  275. asmmode_i386_direct_info : tasmmodeinfo =
  276. (
  277. id : asmmode_i386_direct;
  278. idtxt : 'DIRECT'
  279. );
  280. initialization
  281. RegisterAsmMode(asmmode_i386_direct_info);
  282. end.
  283. {
  284. $Log$
  285. Revision 1.12 2001-11-02 22:58:11 peter
  286. * procsym definition rewrite
  287. Revision 1.11 2001/08/26 13:37:02 florian
  288. * some cg reorganisation
  289. * some PPC updates
  290. Revision 1.10 2001/08/06 21:40:51 peter
  291. * funcret moved from tprocinfo to tprocdef
  292. Revision 1.9 2001/04/18 22:02:03 peter
  293. * registration of targets and assemblers
  294. Revision 1.8 2001/04/13 18:20:21 peter
  295. * scanner object to class
  296. Revision 1.7 2001/04/13 01:22:21 peter
  297. * symtable change to classes
  298. * range check generation and errors fixed, make cycle DEBUG=1 works
  299. * memory leaks fixed
  300. Revision 1.6 2001/04/02 21:20:40 peter
  301. * resulttype rewrite
  302. Revision 1.5 2001/03/11 22:58:52 peter
  303. * getsym redesign, removed the globals srsym,srsymtable
  304. Revision 1.4 2000/12/25 00:07:34 peter
  305. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  306. tlinkedlist objects)
  307. Revision 1.3 2000/11/29 00:30:50 florian
  308. * unused units removed from uses clause
  309. * some changes for widestrings
  310. Revision 1.2 2000/10/31 22:02:57 peter
  311. * symtable splitted, no real code changes
  312. Revision 1.1 2000/10/15 09:47:43 peter
  313. * moved to i386/
  314. Revision 1.5 2000/10/14 10:14:52 peter
  315. * moehrendorf oct 2000 rewrite
  316. Revision 1.4 2000/09/24 15:06:26 peter
  317. * use defines.inc
  318. Revision 1.3 2000/08/27 16:11:52 peter
  319. * moved some util functions from globals,cobjects to cutils
  320. * splitted files into finput,fmodule
  321. Revision 1.2 2000/07/13 11:32:48 michael
  322. + removed logs
  323. }