radirect.pas 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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 radirect;
  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,defutil,paramgr,
  35. { pass 1 }
  36. nbas,
  37. { parser }
  38. scanner,
  39. rax86,
  40. { codegen }
  41. cgbase,
  42. { constants }
  43. itx86att,
  44. cpubase
  45. ;
  46. function assemble : tnode;
  47. var
  48. uhs,
  49. retstr,s,hs : string;
  50. c : char;
  51. ende : boolean;
  52. srsym,sym : tsym;
  53. srsymtable : tsymtable;
  54. code : TAAsmoutput;
  55. framereg : tregister;
  56. i,l : longint;
  57. procedure writeasmline;
  58. var
  59. i : longint;
  60. begin
  61. i:=length(s);
  62. while (i>0) and (s[i] in [' ',#9]) do
  63. dec(i);
  64. s[0]:=chr(i);
  65. if s<>'' then
  66. code.concat(Tai_direct.Create(strpnew(s)));
  67. { consider it set function set if the offset was loaded }
  68. if assigned(current_procdef.funcretsym) and
  69. (pos(retstr,upper(s))>0) then
  70. tvarsym(current_procdef.funcretsym).varstate:=vs_assigned;
  71. s:='';
  72. end;
  73. begin
  74. ende:=false;
  75. s:='';
  76. if assigned(current_procdef.funcretsym) and
  77. is_fpu(current_procdef.rettype.def) then
  78. tvarsym(current_procdef.funcretsym).varstate:=vs_assigned;
  79. framereg:=current_procinfo.framepointer;
  80. convert_register_to_enum(framereg);
  81. if (not is_void(current_procdef.rettype.def)) then
  82. retstr:=upper(tostr(current_procinfo.return_offset)+'('+gas_reg2str[framereg.enum]+')')
  83. else
  84. retstr:='';
  85. c:=current_scanner.asmgetchar;
  86. code:=TAAsmoutput.Create;
  87. while not(ende) do
  88. begin
  89. { wrong placement
  90. current_scanner.gettokenpos; }
  91. case c of
  92. 'A'..'Z','a'..'z','_' : begin
  93. current_scanner.gettokenpos;
  94. i:=0;
  95. hs:='';
  96. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  97. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  98. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  99. or (c='_') do
  100. begin
  101. inc(i);
  102. hs[i]:=c;
  103. c:=current_scanner.asmgetchar;
  104. end;
  105. hs[0]:=chr(i);
  106. if upper(hs)='END' then
  107. ende:=true
  108. else
  109. begin
  110. if c=':' then
  111. begin
  112. searchsym(upper(hs),srsym,srsymtable);
  113. if srsym<>nil then
  114. if (srsym.typ = labelsym) then
  115. Begin
  116. hs:=tlabelsym(srsym).lab.name;
  117. tlabelsym(srsym).lab.is_set:=true;
  118. end
  119. else
  120. Message(asmr_w_using_defined_as_local);
  121. end
  122. else if upper(hs)='FWAIT' then
  123. FwaitWarning
  124. else
  125. { access to local variables }
  126. if assigned(current_procdef) then
  127. begin
  128. { is the last written character an special }
  129. { char ? }
  130. if (s[length(s)]='%') and
  131. (not paramanager.ret_in_param(current_procdef.rettype.def,current_procdef.proccalloption)) and
  132. ((pos('AX',upper(hs))>0) or
  133. (pos('AL',upper(hs))>0)) then
  134. tvarsym(current_procdef.funcretsym).varstate:=vs_assigned;
  135. if (s[length(s)]<>'%') and
  136. (s[length(s)]<>'$') and
  137. (s[length(s)]<>'.') and
  138. ((s[length(s)]<>'0') or (hs[1]<>'x')) then
  139. begin
  140. if assigned(current_procdef.localst) and
  141. (current_procdef.localst.symtablelevel>=normal_function_level) then
  142. sym:=tsym(current_procdef.localst.search(upper(hs)))
  143. else
  144. sym:=nil;
  145. if assigned(sym) then
  146. begin
  147. if (sym.typ = labelsym) then
  148. Begin
  149. hs:=tlabelsym(sym).lab.name;
  150. end
  151. else if sym.typ=varsym then
  152. begin
  153. {variables set are after a comma }
  154. {like in movl %eax,I }
  155. if pos(',',s) > 0 then
  156. tvarsym(sym).varstate:=vs_used
  157. else
  158. if (pos('MOV',upper(s)) > 0) and (tvarsym(sym).varstate=vs_declared) then
  159. Message1(sym_n_uninitialized_local_variable,hs);
  160. if (vo_is_external in tvarsym(sym).varoptions) then
  161. hs:=tvarsym(sym).mangledname
  162. else
  163. hs:='-'+tostr(tvarsym(sym).address)+
  164. '('+gas_reg2str[framereg.enum]+')';
  165. end
  166. else
  167. { call to local function }
  168. if (sym.typ=procsym) and ((pos('CALL',upper(s))>0) or
  169. (pos('LEA',upper(s))>0)) then
  170. begin
  171. hs:=tprocsym(sym).first_procdef.mangledname;
  172. end;
  173. end
  174. else
  175. begin
  176. if assigned(current_procdef.parast) then
  177. sym:=tsym(current_procdef.parast.search(upper(hs)))
  178. else
  179. sym:=nil;
  180. if assigned(sym) then
  181. begin
  182. if sym.typ=varsym then
  183. begin
  184. l:=tvarsym(sym).address;
  185. { set offset }
  186. inc(l,current_procdef.parast.address_fixup);
  187. hs:=tostr(l)+'('+gas_reg2str[framereg.enum]+')';
  188. if pos(',',s) > 0 then
  189. tvarsym(sym).varstate:=vs_used;
  190. end;
  191. end
  192. { I added that but it creates a problem in line.ppi
  193. because there is a local label wbuffer and
  194. a static variable WBUFFER ...
  195. what would you decide, florian ?}
  196. else
  197. begin
  198. uhs:=upper(hs);
  199. if (uhs='__SELF') then
  200. begin
  201. if assigned(current_procdef._class) then
  202. uhs:='self'
  203. else
  204. begin
  205. Message(asmr_e_cannot_use_SELF_outside_a_method);
  206. uhs:='';
  207. end;
  208. end
  209. else
  210. if (uhs='__OLDEBP') then
  211. begin
  212. if current_procdef.parast.symtablelevel>normal_function_level then
  213. uhs:='parentframe'
  214. else
  215. begin
  216. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  217. uhs:='';
  218. end;
  219. end
  220. else
  221. if uhs='__RESULT' then
  222. begin
  223. if (not is_void(current_procdef.rettype.def)) then
  224. uhs:='result'
  225. else
  226. begin
  227. Message(asmr_e_void_function);
  228. uhs:='';
  229. end;
  230. end;
  231. if uhs<>'' then
  232. searchsym(uhs,sym,srsymtable)
  233. else
  234. sym:=nil;
  235. if assigned(sym) then
  236. begin
  237. case sym.owner.symtabletype of
  238. globalsymtable,
  239. staticsymtable :
  240. begin
  241. case sym.typ of
  242. varsym :
  243. begin
  244. Message2(asmr_h_direct_global_to_mangled,hs,tvarsym(sym).mangledname);
  245. hs:=tvarsym(sym).mangledname;
  246. inc(tvarsym(sym).refs);
  247. end;
  248. typedconstsym :
  249. begin
  250. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  251. hs:=ttypedconstsym(sym).mangledname;
  252. end;
  253. procsym :
  254. begin
  255. { procs can be called or the address can be loaded }
  256. if ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  257. begin
  258. if tprocsym(sym).procdef_count>1 then
  259. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  260. Message2(asmr_h_direct_global_to_mangled,hs,tprocsym(sym).first_procdef.mangledname);
  261. hs:=tprocsym(sym).first_procdef.mangledname;
  262. end;
  263. end;
  264. else
  265. Message(asmr_e_wrong_sym_type);
  266. end;
  267. end;
  268. parasymtable,
  269. localsymtable :
  270. begin
  271. case sym.typ of
  272. varsym :
  273. begin
  274. hs:=tostr(tvarsym(sym).adjusted_address)+
  275. '('+gas_reg2str[framereg.enum]+')';
  276. inc(tvarsym(sym).refs);
  277. end;
  278. typedconstsym :
  279. begin
  280. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  281. hs:=ttypedconstsym(sym).mangledname;
  282. end;
  283. else
  284. Message(asmr_e_wrong_sym_type);
  285. end;
  286. end;
  287. end;
  288. end
  289. end;
  290. end;
  291. end;
  292. end;
  293. s:=s+hs;
  294. end;
  295. end;
  296. '{',';',#10,#13 :
  297. begin
  298. if pos(retstr,s) > 0 then
  299. tvarsym(current_procdef.funcretsym).varstate:=vs_assigned;
  300. writeasmline;
  301. c:=current_scanner.asmgetchar;
  302. end;
  303. #26 :
  304. Message(scan_f_end_of_file);
  305. else
  306. begin
  307. current_scanner.gettokenpos;
  308. inc(byte(s[0]));
  309. s[length(s)]:=c;
  310. c:=current_scanner.asmgetchar;
  311. end;
  312. end;
  313. end;
  314. writeasmline;
  315. assemble:=casmnode.create(code);
  316. end;
  317. {*****************************************************************************
  318. Initialize
  319. *****************************************************************************}
  320. const
  321. {$ifdef x86_64}
  322. asmmode_x86_64_direct_info : tasmmodeinfo =
  323. (
  324. id : asmmode_direct;
  325. idtxt : 'DIRECT'
  326. );
  327. {$else x86_64}
  328. asmmode_i386_direct_info : tasmmodeinfo =
  329. (
  330. id : asmmode_direct;
  331. idtxt : 'DIRECT'
  332. );
  333. {$endif x86_64}
  334. initialization
  335. {$ifdef x86_64}
  336. RegisterAsmMode(asmmode_x86_64_direct_info);
  337. {$else x86_64}
  338. RegisterAsmMode(asmmode_i386_direct_info);
  339. {$endif x86_64}
  340. end.
  341. {
  342. $Log$
  343. Revision 1.5 2003-05-22 21:33:31 peter
  344. * removed some unit dependencies
  345. Revision 1.4 2003/05/15 18:58:54 peter
  346. * removed selfpointer_offset, vmtpointer_offset
  347. * tvarsym.adjusted_address
  348. * address in localsymtable is now in the real direction
  349. * removed some obsolete globals
  350. Revision 1.3 2003/05/13 19:15:28 peter
  351. * removed radirect
  352. Revision 1.2 2003/05/01 07:59:43 florian
  353. * introduced defaultordconsttype to decribe the default size of ordinal constants
  354. on 64 bit CPUs it's equal to cs64bitdef while on 32 bit CPUs it's equal to s32bitdef
  355. + added defines CPU32 and CPU64 for 32 bit and 64 bit CPUs
  356. * int64s/qwords are allowed as for loop counter on 64 bit CPUs
  357. Revision 1.1 2003/04/30 15:45:35 florian
  358. * merged more x86-64/i386 code
  359. Revision 1.11 2003/04/27 11:21:36 peter
  360. * aktprocdef renamed to current_procdef
  361. * procinfo renamed to current_procinfo
  362. * procinfo will now be stored in current_module so it can be
  363. cleaned up properly
  364. * gen_main_procsym changed to create_main_proc and release_main_proc
  365. to also generate a tprocinfo structure
  366. * fixed unit implicit initfinal
  367. Revision 1.10 2003/04/27 07:29:52 peter
  368. * current_procdef cleanup, current_procdef is now always nil when parsing
  369. a new procdef declaration
  370. * aktprocsym removed
  371. * lexlevel removed, use symtable.symtablelevel instead
  372. * implicit init/final code uses the normal genentry/genexit
  373. * funcret state checking updated for new funcret handling
  374. Revision 1.9 2003/04/25 20:59:35 peter
  375. * removed funcretn,funcretsym, function result is now in varsym
  376. and aliases for result and function name are added using absolutesym
  377. * vs_hidden parameter for funcret passed in parameter
  378. * vs_hidden fixes
  379. * writenode changed to printnode and released from extdebug
  380. * -vp option added to generate a tree.log with the nodetree
  381. * nicer printnode for statements, callnode
  382. Revision 1.8 2003/04/25 12:04:31 florian
  383. * merged agx64att and ag386att to x86/agx86att
  384. Revision 1.7 2003/04/21 20:05:10 peter
  385. * removed some ie checks
  386. Revision 1.6 2003/01/08 18:43:57 daniel
  387. * Tregister changed into a record
  388. Revision 1.5 2002/11/25 17:43:27 peter
  389. * splitted defbase in defutil,symutil,defcmp
  390. * merged isconvertable and is_equal into compare_defs(_ext)
  391. * made operator search faster by walking the list only once
  392. Revision 1.4 2002/11/18 17:32:00 peter
  393. * pass proccalloption to ret_in_xxx and push_xxx functions
  394. Revision 1.3 2002/09/03 16:26:28 daniel
  395. * Make Tprocdef.defs protected
  396. Revision 1.2 2002/08/17 09:23:47 florian
  397. * first part of procinfo rewrite
  398. Revision 1.1 2002/08/10 14:47:50 carl
  399. + moved target_cpu_string to cpuinfo
  400. * renamed asmmode enum.
  401. * assembler reader has now less ifdef's
  402. * move from nppcmem.pas -> ncgmem.pas vec. node.
  403. Revision 1.21 2002/07/20 11:58:05 florian
  404. * types.pas renamed to defbase.pas because D6 contains a types
  405. unit so this would conflicts if D6 programms are compiled
  406. + Willamette/SSE2 instructions to assembler added
  407. Revision 1.20 2002/07/11 14:41:34 florian
  408. * start of the new generic parameter handling
  409. Revision 1.19 2002/07/01 18:46:34 peter
  410. * internal linker
  411. * reorganized aasm layer
  412. Revision 1.18 2002/05/18 13:34:26 peter
  413. * readded missing revisions
  414. Revision 1.17 2002/05/16 19:46:52 carl
  415. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  416. + try to fix temp allocation (still in ifdef)
  417. + generic constructor calls
  418. + start of tassembler / tmodulebase class cleanup
  419. Revision 1.15 2002/05/12 16:53:18 peter
  420. * moved entry and exitcode to ncgutil and cgobj
  421. * foreach gets extra argument for passing local data to the
  422. iterator function
  423. * -CR checks also class typecasts at runtime by changing them
  424. into as
  425. * fixed compiler to cycle with the -CR option
  426. * fixed stabs with elf writer, finally the global variables can
  427. be watched
  428. * removed a lot of routines from cga unit and replaced them by
  429. calls to cgobj
  430. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  431. u32bit then the other is typecasted also to u32bit without giving
  432. a rangecheck warning/error.
  433. * fixed pascal calling method with reversing also the high tree in
  434. the parast, detected by tcalcst3 test
  435. Revision 1.14 2002/04/15 19:12:09 carl
  436. + target_info.size_of_pointer -> pointer_size
  437. + some cleanup of unused types/variables
  438. * move several constants from cpubase to their specific units
  439. (where they are used)
  440. + att_Reg2str -> gas_reg2str
  441. + int_reg2str -> std_reg2str
  442. Revision 1.13 2002/04/14 17:01:52 carl
  443. + att_reg2str -> gas_reg2str
  444. }