radirect.pas 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. {*****************************************************************************}
  2. { File : radirect.pas }
  3. { Author : Mazen NEIFER }
  4. { Project : Free Pascal Compiler (FPC) }
  5. { Creation date : 2002\08\22 }
  6. { Last modification date : 2002\08\22 }
  7. { Licence : GPL }
  8. { Bug report : [email protected] }
  9. {*****************************************************************************}
  10. {
  11. $Id$
  12. Copyright (c) 1998-2002 by Florian Klaempfl
  13. Reads inline assembler and writes the lines direct to the output
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation; either version 2 of the License, or
  17. (at your option) any later version.
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program; if not, write to the Free Software
  24. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. ****************************************************************************}
  26. unit radirect;
  27. {$MACRO ON}{$i fpcdefs.inc}
  28. interface
  29. uses
  30. node;
  31. function assemble : tnode;
  32. implementation
  33. uses
  34. { common }
  35. cutils,
  36. { global }
  37. globals,verbose,
  38. systems,
  39. { aasm }
  40. aasmbase,aasmtai,aasmcpu,
  41. { symtable }
  42. symconst,symbase,symtype,symsym,symtable,defbase,paramgr,
  43. { pass 1 }
  44. nbas,
  45. { parser }
  46. scanner,
  47. rautils,
  48. { codegen }
  49. cgbase,
  50. { constants }
  51. aggas,cpubase,globtype
  52. ;
  53. Procedure FWaitWarning;
  54. begin
  55. if (target_info.system=system_i386_GO32V2) and (cs_fp_emulation in aktmoduleswitches) then
  56. Message(asmr_w_fwait_emu_prob);
  57. end;
  58. function assemble : tnode;
  59. var
  60. retstr,s,hs : string;
  61. c : char;
  62. ende : boolean;
  63. srsym,sym : tsym;
  64. srsymtable : tsymtable;
  65. code : TAAsmoutput;
  66. i,l : longint;
  67. procedure writeasmline;
  68. var
  69. i : longint;
  70. begin
  71. i:=length(s);
  72. while (i>0) and (s[i] in [' ',#9]) do
  73. dec(i);
  74. s[0]:=chr(i);
  75. if s<>'' then
  76. code.concat(Tai_direct.Create(strpnew(s)));
  77. { consider it set function set if the offset was loaded }
  78. if assigned(aktprocdef.funcretsym) and
  79. (pos(retstr,upper(s))>0) then
  80. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  81. s:='';
  82. end;
  83. begin
  84. ende:=false;
  85. s:='';
  86. if assigned(aktprocdef.funcretsym) and
  87. is_fpu(aktprocdef.rettype.def) then
  88. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  89. if (not is_void(aktprocdef.rettype.def)) then
  90. retstr:=upper(tostr(procinfo.return_offset)+'('+std_reg2str[procinfo.framepointer]+')')
  91. else
  92. retstr:='';
  93. c:=current_scanner.asmgetchar;
  94. code:=TAAsmoutput.Create;
  95. while not(ende) do
  96. begin
  97. { wrong placement
  98. current_scanner.gettokenpos; }
  99. case c of
  100. 'A'..'Z','a'..'z','_' : begin
  101. current_scanner.gettokenpos;
  102. i:=0;
  103. hs:='';
  104. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  105. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  106. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  107. or (c='_') do
  108. begin
  109. inc(i);
  110. hs[i]:=c;
  111. c:=current_scanner.asmgetchar;
  112. end;
  113. hs[0]:=chr(i);
  114. if upper(hs)='END' then
  115. ende:=true
  116. else
  117. begin
  118. if c=':' then
  119. begin
  120. searchsym(upper(hs),srsym,srsymtable);
  121. if srsym<>nil then
  122. if (srsym.typ = labelsym) then
  123. Begin
  124. hs:=tlabelsym(srsym).lab.name;
  125. tlabelsym(srsym).lab.is_set:=true;
  126. end
  127. else
  128. Message(asmr_w_using_defined_as_local);
  129. end
  130. else if upper(hs)='FWAIT' then
  131. FwaitWarning
  132. else
  133. { access to local variables }
  134. if assigned(aktprocdef) then
  135. begin
  136. { is the last written character an special }
  137. { char ? }
  138. if (s[length(s)]='%') and
  139. paramanager.ret_in_acc(aktprocdef.rettype.def,aktprocdef.proccalloption) and
  140. ((pos('AX',upper(hs))>0) or
  141. (pos('AL',upper(hs))>0)) then
  142. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  143. if (s[length(s)]<>'%') and
  144. (s[length(s)]<>'$') and
  145. ((s[length(s)]<>'0') or (hs[1]<>'x')) then
  146. begin
  147. if assigned(aktprocdef.localst) and
  148. (lexlevel >= normal_function_level) then
  149. sym:=tsym(aktprocdef.localst.search(upper(hs)))
  150. else
  151. sym:=nil;
  152. if assigned(sym) then
  153. begin
  154. if (sym.typ = labelsym) then
  155. Begin
  156. hs:=tlabelsym(sym).lab.name;
  157. end
  158. else if sym.typ=varsym then
  159. begin
  160. {variables set are after a comma }
  161. {like in movl %eax,I }
  162. if pos(',',s) > 0 then
  163. tvarsym(sym).varstate:=vs_used
  164. else
  165. if (pos('MOV',upper(s)) > 0) and (tvarsym(sym).varstate=vs_declared) then
  166. Message1(sym_n_uninitialized_local_variable,hs);
  167. if (vo_is_external in tvarsym(sym).varoptions) then
  168. hs:=tvarsym(sym).mangledname
  169. else
  170. hs:='-'+tostr(tvarsym(sym).address)+
  171. '('+std_reg2str[procinfo.framepointer]+')';
  172. end
  173. else
  174. { call to local function }
  175. if (sym.typ=procsym) and ((pos('CALL',upper(s))>0) or
  176. (pos('LEA',upper(s))>0)) then
  177. begin
  178. hs:=tprocsym(sym).first_procdef.mangledname;
  179. end;
  180. end
  181. else
  182. begin
  183. if assigned(aktprocdef.parast) then
  184. sym:=tsym(aktprocdef.parast.search(upper(hs)))
  185. else
  186. sym:=nil;
  187. if assigned(sym) then
  188. begin
  189. if sym.typ=varsym then
  190. begin
  191. l:=tvarsym(sym).address;
  192. { set offset }
  193. inc(l,aktprocdef.parast.address_fixup);
  194. hs:=tostr(l)+'('+std_reg2str[procinfo.framepointer]+')';
  195. if pos(',',s) > 0 then
  196. tvarsym(sym).varstate:=vs_used;
  197. end;
  198. end
  199. { I added that but it creates a problem in line.ppi
  200. because there is a local label wbuffer and
  201. a static variable WBUFFER ...
  202. what would you decide, florian ?}
  203. else
  204. begin
  205. searchsym(upper(hs),sym,srsymtable);
  206. if assigned(sym) and (sym.owner.symtabletype in [globalsymtable,staticsymtable]) then
  207. begin
  208. case sym.typ of
  209. varsym :
  210. begin
  211. Message2(asmr_h_direct_global_to_mangled,hs,tvarsym(sym).mangledname);
  212. hs:=tvarsym(sym).mangledname;
  213. inc(tvarsym(sym).refs);
  214. end;
  215. typedconstsym :
  216. begin
  217. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  218. hs:=ttypedconstsym(sym).mangledname;
  219. end;
  220. procsym :
  221. begin
  222. { procs can be called or the address can be loaded }
  223. if ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  224. begin
  225. if assigned(tprocsym(sym).first_procdef) then
  226. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  227. Message2(asmr_h_direct_global_to_mangled,hs,tprocsym(sym).first_procdef.mangledname);
  228. hs:=tprocsym(sym).first_procdef.mangledname;
  229. end;
  230. end;
  231. else
  232. Message(asmr_e_wrong_sym_type);
  233. end;
  234. end
  235. else if upper(hs)='__SELF' then
  236. begin
  237. if assigned(procinfo._class) then
  238. hs:=tostr(procinfo.selfpointer_offset)+
  239. '('+std_reg2str[procinfo.framepointer]+')'
  240. else
  241. Message(asmr_e_cannot_use_SELF_outside_a_method);
  242. end
  243. else if upper(hs)='__RESULT' then
  244. begin
  245. if (not is_void(aktprocdef.rettype.def)) then
  246. hs:=retstr
  247. else
  248. Message(asmr_e_void_function);
  249. end
  250. else if upper(hs)='__OLDEBP' then
  251. begin
  252. { complicate to check there }
  253. { we do it: }
  254. if lexlevel>normal_function_level then
  255. hs:=tostr(procinfo.framepointer_offset)+
  256. '('+std_reg2str[procinfo.framepointer]+')'
  257. else
  258. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  259. end;
  260. end;
  261. end;
  262. end;
  263. end;
  264. s:=s+hs;
  265. end;
  266. end;
  267. '{',';',#10,#13 : begin
  268. if pos(retstr,s) > 0 then
  269. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  270. writeasmline;
  271. c:=current_scanner.asmgetchar;
  272. end;
  273. #26 : Message(scan_f_end_of_file);
  274. else
  275. begin
  276. current_scanner.gettokenpos;
  277. inc(byte(s[0]));
  278. s[length(s)]:=c;
  279. c:=current_scanner.asmgetchar;
  280. end;
  281. end;
  282. end;
  283. writeasmline;
  284. assemble:=casmnode.create(code);
  285. end;
  286. {*****************************************************************************
  287. Initialize
  288. *****************************************************************************}
  289. const
  290. asmmode_i386_direct_info : tasmmodeinfo =
  291. (
  292. id : asmmode_direct;
  293. idtxt : 'DIRECT'
  294. );
  295. initialization
  296. RegisterAsmMode(asmmode_i386_direct_info);
  297. end.
  298. {
  299. $Log$
  300. Revision 1.3 2002-11-18 17:32:01 peter
  301. * pass proccalloption to ret_in_xxx and push_xxx functions
  302. Revision 1.2 2002/09/19 20:24:47 mazen
  303. + call support
  304. Revision 1.1 2002/08/23 10:08:28 mazen
  305. *** empty log message ***
  306. Revision 1.2 2002/08/17 09:23:47 florian
  307. * first part of procinfo rewrite
  308. Revision 1.1 2002/08/10 14:47:50 carl
  309. + moved target_cpu_string to cpuinfo
  310. * renamed asmmode enum.
  311. * assembler reader has now less ifdef's
  312. * move from nppcmem.pas -> ncgmem.pas vec. node.
  313. Revision 1.21 2002/07/20 11:58:05 florian
  314. * types.pas renamed to defbase.pas because D6 contains a types
  315. unit so this would conflicts if D6 programms are compiled
  316. + Willamette/SSE2 instructions to assembler added
  317. Revision 1.20 2002/07/11 14:41:34 florian
  318. * start of the new generic parameter handling
  319. Revision 1.19 2002/07/01 18:46:34 peter
  320. * internal linker
  321. * reorganized aasm layer
  322. Revision 1.18 2002/05/18 13:34:26 peter
  323. * readded missing revisions
  324. Revision 1.17 2002/05/16 19:46:52 carl
  325. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  326. + try to fix temp allocation (still in ifdef)
  327. + generic constructor calls
  328. + start of tassembler / tmodulebase class cleanup
  329. Revision 1.15 2002/05/12 16:53:18 peter
  330. * moved entry and exitcode to ncgutil and cgobj
  331. * foreach gets extra argument for passing local data to the
  332. iterator function
  333. * -CR checks also class typecasts at runtime by changing them
  334. into as
  335. * fixed compiler to cycle with the -CR option
  336. * fixed stabs with elf writer, finally the global variables can
  337. be watched
  338. * removed a lot of routines from cga unit and replaced them by
  339. calls to cgobj
  340. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  341. u32bit then the other is typecasted also to u32bit without giving
  342. a rangecheck warning/error.
  343. * fixed pascal calling method with reversing also the high tree in
  344. the parast, detected by tcalcst3 test
  345. Revision 1.14 2002/04/15 19:12:09 carl
  346. + target_info.size_of_pointer -> pointer_size
  347. + some cleanup of unused types/variables
  348. * move several constants from cpubase to their specific units
  349. (where they are used)
  350. + att_Reg2str -> std_reg2str
  351. + int_reg2str -> std_reg2str
  352. Revision 1.13 2002/04/14 17:01:52 carl
  353. + att_reg2str -> std_reg2str
  354. }