ra386dir.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. {$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 : tsym;
  53. srsymtable : tsymtable;
  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. (not is_void(procinfo^.returntype.def)) 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:=tlabelsym(srsym).lab.name;
  115. tlabelsym(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:=tsym(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:=tlabelsym(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. tvarsym(sym).varstate:=vs_used
  154. else
  155. if (pos('MOV',upper(s)) > 0) and (tvarsym(sym).varstate=vs_declared) then
  156. Message1(sym_n_uninitialized_local_variable,hs);
  157. if (vo_is_external in tvarsym(sym).varoptions) then
  158. hs:=tvarsym(sym).mangledname
  159. else
  160. hs:='-'+tostr(tvarsym(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:=tprocsym(sym).definition.mangledname;
  169. end;
  170. end
  171. else
  172. begin
  173. if assigned(aktprocsym.definition.parast) then
  174. sym:=tsym(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:=tvarsym(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. tvarsym(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 [globalsymtable,staticsymtable]) then
  198. begin
  199. if (sym.typ = varsym) or (sym.typ = typedconstsym) then
  200. begin
  201. Message2(asmr_h_direct_global_to_mangled,hs,sym.mangledname);
  202. hs:=sym.mangledname;
  203. if sym.typ=varsym then
  204. inc(tvarsym(sym).refs);
  205. end;
  206. { procs can be called or the address can be loaded }
  207. if (sym.typ=procsym) and
  208. ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  209. begin
  210. if assigned(tprocsym(sym).definition.nextoverloaded) then
  211. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  212. Message2(asmr_h_direct_global_to_mangled,hs,sym.mangledname);
  213. hs:=sym.mangledname;
  214. end;
  215. end
  216. else
  217. {$endif TESTGLOBALVAR}
  218. if upper(hs)='__SELF' then
  219. begin
  220. if assigned(procinfo^._class) then
  221. hs:=tostr(procinfo^.selfpointer_offset)+
  222. '('+att_reg2str[procinfo^.framepointer]+')'
  223. else
  224. Message(asmr_e_cannot_use_SELF_outside_a_method);
  225. end
  226. else if upper(hs)='__RESULT' then
  227. begin
  228. if assigned(procinfo^.returntype.def) and
  229. (not is_void(procinfo^.returntype.def)) then
  230. hs:=retstr
  231. else
  232. Message(asmr_e_void_function);
  233. end
  234. else if upper(hs)='__OLDEBP' then
  235. begin
  236. { complicate to check there }
  237. { we do it: }
  238. if lexlevel>normal_function_level then
  239. hs:=tostr(procinfo^.framepointer_offset)+
  240. '('+att_reg2str[procinfo^.framepointer]+')'
  241. else
  242. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  243. end;
  244. end;
  245. end;
  246. end;
  247. end;
  248. s:=s+hs;
  249. end;
  250. end;
  251. '{',';',#10,#13 : begin
  252. if pos(retstr,s) > 0 then
  253. procinfo^.funcret_state:=vs_assigned;
  254. writeasmline;
  255. c:=current_scanner.asmgetchar;
  256. end;
  257. #26 : Message(scan_f_end_of_file);
  258. else
  259. begin
  260. current_scanner.gettokenpos;
  261. inc(byte(s[0]));
  262. s[length(s)]:=c;
  263. c:=current_scanner.asmgetchar;
  264. end;
  265. end;
  266. end;
  267. writeasmline;
  268. assemble:=casmnode.create(code);
  269. end;
  270. {*****************************************************************************
  271. Initialize
  272. *****************************************************************************}
  273. const
  274. asmmode_i386_direct_info : tasmmodeinfo =
  275. (
  276. id : asmmode_i386_direct;
  277. idtxt : 'DIRECT'
  278. );
  279. initialization
  280. RegisterAsmMode(asmmode_i386_direct_info);
  281. end.
  282. {
  283. $Log$
  284. Revision 1.9 2001-04-18 22:02:03 peter
  285. * registration of targets and assemblers
  286. Revision 1.8 2001/04/13 18:20:21 peter
  287. * scanner object to class
  288. Revision 1.7 2001/04/13 01:22:21 peter
  289. * symtable change to classes
  290. * range check generation and errors fixed, make cycle DEBUG=1 works
  291. * memory leaks fixed
  292. Revision 1.6 2001/04/02 21:20:40 peter
  293. * resulttype rewrite
  294. Revision 1.5 2001/03/11 22:58:52 peter
  295. * getsym redesign, removed the globals srsym,srsymtable
  296. Revision 1.4 2000/12/25 00:07:34 peter
  297. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  298. tlinkedlist objects)
  299. Revision 1.3 2000/11/29 00:30:50 florian
  300. * unused units removed from uses clause
  301. * some changes for widestrings
  302. Revision 1.2 2000/10/31 22:02:57 peter
  303. * symtable splitted, no real code changes
  304. Revision 1.1 2000/10/15 09:47:43 peter
  305. * moved to i386/
  306. Revision 1.5 2000/10/14 10:14:52 peter
  307. * moehrendorf oct 2000 rewrite
  308. Revision 1.4 2000/09/24 15:06:26 peter
  309. * use defines.inc
  310. Revision 1.3 2000/08/27 16:11:52 peter
  311. * moved some util functions from globals,cobjects to cutils
  312. * splitted files into finput,fmodule
  313. Revision 1.2 2000/07/13 11:32:48 michael
  314. + removed logs
  315. }