radirect.pas 22 KB

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