radirect.pas 21 KB

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