ra386dir.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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 fpcdefs.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. aasmbase,aasmtai,aasmcpu,
  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. { constants }
  43. ag386att
  44. ;
  45. function assemble : tnode;
  46. var
  47. retstr,s,hs : string;
  48. c : char;
  49. ende : boolean;
  50. srsym,sym : tsym;
  51. srsymtable : tsymtable;
  52. code : TAAsmoutput;
  53. i,l : longint;
  54. procedure writeasmline;
  55. var
  56. i : longint;
  57. begin
  58. i:=length(s);
  59. while (i>0) and (s[i] in [' ',#9]) do
  60. dec(i);
  61. s[0]:=chr(i);
  62. if s<>'' then
  63. code.concat(Tai_direct.Create(strpnew(s)));
  64. { consider it set function set if the offset was loaded }
  65. if assigned(aktprocdef.funcretsym) and
  66. (pos(retstr,upper(s))>0) then
  67. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  68. s:='';
  69. end;
  70. begin
  71. ende:=false;
  72. s:='';
  73. if assigned(aktprocdef.funcretsym) and
  74. is_fpu(aktprocdef.rettype.def) then
  75. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  76. if (not is_void(aktprocdef.rettype.def)) then
  77. retstr:=upper(tostr(procinfo^.return_offset)+'('+gas_reg2str[procinfo^.framepointer]+')')
  78. else
  79. retstr:='';
  80. c:=current_scanner.asmgetchar;
  81. code:=TAAsmoutput.Create;
  82. while not(ende) do
  83. begin
  84. { wrong placement
  85. current_scanner.gettokenpos; }
  86. case c of
  87. 'A'..'Z','a'..'z','_' : begin
  88. current_scanner.gettokenpos;
  89. i:=0;
  90. hs:='';
  91. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  92. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  93. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  94. or (c='_') do
  95. begin
  96. inc(i);
  97. hs[i]:=c;
  98. c:=current_scanner.asmgetchar;
  99. end;
  100. hs[0]:=chr(i);
  101. if upper(hs)='END' then
  102. ende:=true
  103. else
  104. begin
  105. if c=':' then
  106. begin
  107. searchsym(upper(hs),srsym,srsymtable);
  108. if srsym<>nil then
  109. if (srsym.typ = labelsym) then
  110. Begin
  111. hs:=tlabelsym(srsym).lab.name;
  112. tlabelsym(srsym).lab.is_set:=true;
  113. end
  114. else
  115. Message(asmr_w_using_defined_as_local);
  116. end
  117. else if upper(hs)='FWAIT' then
  118. FwaitWarning
  119. else
  120. { access to local variables }
  121. if assigned(aktprocdef) then
  122. begin
  123. { is the last written character an special }
  124. { char ? }
  125. if (s[length(s)]='%') and
  126. ret_in_acc(aktprocdef.rettype.def) and
  127. ((pos('AX',upper(hs))>0) or
  128. (pos('AL',upper(hs))>0)) then
  129. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  130. if (s[length(s)]<>'%') and
  131. (s[length(s)]<>'$') and
  132. ((s[length(s)]<>'0') or (hs[1]<>'x')) then
  133. begin
  134. if assigned(aktprocdef.localst) and
  135. (lexlevel >= normal_function_level) then
  136. sym:=tsym(aktprocdef.localst.search(upper(hs)))
  137. else
  138. sym:=nil;
  139. if assigned(sym) then
  140. begin
  141. if (sym.typ = labelsym) then
  142. Begin
  143. hs:=tlabelsym(sym).lab.name;
  144. end
  145. else if sym.typ=varsym then
  146. begin
  147. {variables set are after a comma }
  148. {like in movl %eax,I }
  149. if pos(',',s) > 0 then
  150. tvarsym(sym).varstate:=vs_used
  151. else
  152. if (pos('MOV',upper(s)) > 0) and (tvarsym(sym).varstate=vs_declared) then
  153. Message1(sym_n_uninitialized_local_variable,hs);
  154. if (vo_is_external in tvarsym(sym).varoptions) then
  155. hs:=tvarsym(sym).mangledname
  156. else
  157. hs:='-'+tostr(tvarsym(sym).address)+
  158. '('+gas_reg2str[procinfo^.framepointer]+')';
  159. end
  160. else
  161. { call to local function }
  162. if (sym.typ=procsym) and ((pos('CALL',upper(s))>0) or
  163. (pos('LEA',upper(s))>0)) then
  164. begin
  165. hs:=tprocsym(sym).defs^.def.mangledname;
  166. end;
  167. end
  168. else
  169. begin
  170. if assigned(aktprocdef.parast) then
  171. sym:=tsym(aktprocdef.parast.search(upper(hs)))
  172. else
  173. sym:=nil;
  174. if assigned(sym) then
  175. begin
  176. if sym.typ=varsym then
  177. begin
  178. l:=tvarsym(sym).address;
  179. { set offset }
  180. inc(l,aktprocdef.parast.address_fixup);
  181. hs:=tostr(l)+'('+gas_reg2str[procinfo^.framepointer]+')';
  182. if pos(',',s) > 0 then
  183. tvarsym(sym).varstate:=vs_used;
  184. end;
  185. end
  186. { I added that but it creates a problem in line.ppi
  187. because there is a local label wbuffer and
  188. a static variable WBUFFER ...
  189. what would you decide, florian ?}
  190. else
  191. begin
  192. searchsym(upper(hs),sym,srsymtable);
  193. if assigned(sym) and (sym.owner.symtabletype in [globalsymtable,staticsymtable]) then
  194. begin
  195. case sym.typ of
  196. varsym :
  197. begin
  198. Message2(asmr_h_direct_global_to_mangled,hs,tvarsym(sym).mangledname);
  199. hs:=tvarsym(sym).mangledname;
  200. inc(tvarsym(sym).refs);
  201. end;
  202. typedconstsym :
  203. begin
  204. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  205. hs:=ttypedconstsym(sym).mangledname;
  206. end;
  207. procsym :
  208. begin
  209. { procs can be called or the address can be loaded }
  210. if ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  211. begin
  212. if assigned(tprocsym(sym).defs^.def) then
  213. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  214. Message2(asmr_h_direct_global_to_mangled,hs,tprocsym(sym).defs^.def.mangledname);
  215. hs:=tprocsym(sym).defs^.def.mangledname;
  216. end;
  217. end;
  218. else
  219. Message(asmr_e_wrong_sym_type);
  220. end;
  221. end
  222. else if upper(hs)='__SELF' then
  223. begin
  224. if assigned(procinfo^._class) then
  225. hs:=tostr(procinfo^.selfpointer_offset)+
  226. '('+gas_reg2str[procinfo^.framepointer]+')'
  227. else
  228. Message(asmr_e_cannot_use_SELF_outside_a_method);
  229. end
  230. else if upper(hs)='__RESULT' then
  231. begin
  232. if (not is_void(aktprocdef.rettype.def)) then
  233. hs:=retstr
  234. else
  235. Message(asmr_e_void_function);
  236. end
  237. else if upper(hs)='__OLDEBP' then
  238. begin
  239. { complicate to check there }
  240. { we do it: }
  241. if lexlevel>normal_function_level then
  242. hs:=tostr(procinfo^.framepointer_offset)+
  243. '('+gas_reg2str[procinfo^.framepointer]+')'
  244. else
  245. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  246. end;
  247. end;
  248. end;
  249. end;
  250. end;
  251. s:=s+hs;
  252. end;
  253. end;
  254. '{',';',#10,#13 : begin
  255. if pos(retstr,s) > 0 then
  256. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  257. writeasmline;
  258. c:=current_scanner.asmgetchar;
  259. end;
  260. #26 : Message(scan_f_end_of_file);
  261. else
  262. begin
  263. current_scanner.gettokenpos;
  264. inc(byte(s[0]));
  265. s[length(s)]:=c;
  266. c:=current_scanner.asmgetchar;
  267. end;
  268. end;
  269. end;
  270. writeasmline;
  271. assemble:=casmnode.create(code);
  272. end;
  273. {*****************************************************************************
  274. Initialize
  275. *****************************************************************************}
  276. const
  277. asmmode_i386_direct_info : tasmmodeinfo =
  278. (
  279. id : asmmode_i386_direct;
  280. idtxt : 'DIRECT'
  281. );
  282. initialization
  283. RegisterAsmMode(asmmode_i386_direct_info);
  284. end.
  285. {
  286. $Log$
  287. Revision 1.19 2002-07-01 18:46:34 peter
  288. * internal linker
  289. * reorganized aasm layer
  290. Revision 1.18 2002/05/18 13:34:26 peter
  291. * readded missing revisions
  292. Revision 1.17 2002/05/16 19:46:52 carl
  293. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  294. + try to fix temp allocation (still in ifdef)
  295. + generic constructor calls
  296. + start of tassembler / tmodulebase class cleanup
  297. Revision 1.15 2002/05/12 16:53:18 peter
  298. * moved entry and exitcode to ncgutil and cgobj
  299. * foreach gets extra argument for passing local data to the
  300. iterator function
  301. * -CR checks also class typecasts at runtime by changing them
  302. into as
  303. * fixed compiler to cycle with the -CR option
  304. * fixed stabs with elf writer, finally the global variables can
  305. be watched
  306. * removed a lot of routines from cga unit and replaced them by
  307. calls to cgobj
  308. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  309. u32bit then the other is typecasted also to u32bit without giving
  310. a rangecheck warning/error.
  311. * fixed pascal calling method with reversing also the high tree in
  312. the parast, detected by tcalcst3 test
  313. Revision 1.14 2002/04/15 19:12:09 carl
  314. + target_info.size_of_pointer -> pointer_size
  315. + some cleanup of unused types/variables
  316. * move several constants from cpubase to their specific units
  317. (where they are used)
  318. + att_Reg2str -> gas_reg2str
  319. + int_reg2str -> std_reg2str
  320. Revision 1.13 2002/04/14 17:01:52 carl
  321. + att_reg2str -> gas_reg2str
  322. }