radirect.pas 22 KB

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