radirect.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. agx86att,
  44. cpubase
  45. ;
  46. function assemble : tnode;
  47. var
  48. retstr,s,hs : string;
  49. c : char;
  50. ende : boolean;
  51. srsym,sym : tsym;
  52. srsymtable : tsymtable;
  53. code : TAAsmoutput;
  54. framereg : tregister;
  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. { consider it set function set if the offset was loaded }
  67. if assigned(current_procdef.funcretsym) and
  68. (pos(retstr,upper(s))>0) then
  69. tvarsym(current_procdef.funcretsym).varstate:=vs_assigned;
  70. s:='';
  71. end;
  72. begin
  73. ende:=false;
  74. s:='';
  75. if assigned(current_procdef.funcretsym) and
  76. is_fpu(current_procdef.rettype.def) then
  77. tvarsym(current_procdef.funcretsym).varstate:=vs_assigned;
  78. framereg:=current_procinfo.framepointer;
  79. convert_register_to_enum(framereg);
  80. if (not is_void(current_procdef.rettype.def)) then
  81. retstr:=upper(tostr(current_procinfo.return_offset)+'('+gas_reg2str[framereg.enum]+')')
  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_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_procdef.rettype.def,current_procdef.proccalloption)) and
  131. ((pos('AX',upper(hs))>0) or
  132. (pos('AL',upper(hs))>0)) then
  133. tvarsym(current_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_procdef.localst) and
  140. (current_procdef.localst.symtablelevel>=normal_function_level) then
  141. sym:=tsym(current_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_reg2str[framereg.enum]+')';
  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_procdef.parast) then
  176. sym:=tsym(current_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_procdef.parast.address_fixup);
  186. hs:=tostr(l)+'('+gas_reg2str[framereg.enum]+')';
  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. searchsym(upper(hs),sym,srsymtable);
  198. if assigned(sym) and (sym.owner.symtabletype in [globalsymtable,staticsymtable]) then
  199. begin
  200. case sym.typ of
  201. varsym :
  202. begin
  203. Message2(asmr_h_direct_global_to_mangled,hs,tvarsym(sym).mangledname);
  204. hs:=tvarsym(sym).mangledname;
  205. inc(tvarsym(sym).refs);
  206. end;
  207. typedconstsym :
  208. begin
  209. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  210. hs:=ttypedconstsym(sym).mangledname;
  211. end;
  212. procsym :
  213. begin
  214. { procs can be called or the address can be loaded }
  215. if ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  216. begin
  217. if tprocsym(sym).procdef_count>1 then
  218. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  219. Message2(asmr_h_direct_global_to_mangled,hs,tprocsym(sym).first_procdef.mangledname);
  220. hs:=tprocsym(sym).first_procdef.mangledname;
  221. end;
  222. end;
  223. else
  224. Message(asmr_e_wrong_sym_type);
  225. end;
  226. end
  227. else if upper(hs)='__SELF' then
  228. begin
  229. if assigned(current_procdef._class) then
  230. hs:=tostr(current_procinfo.selfpointer_offset)+
  231. '('+gas_reg2str[framereg.enum]+')'
  232. else
  233. Message(asmr_e_cannot_use_SELF_outside_a_method);
  234. end
  235. else if upper(hs)='__RESULT' then
  236. begin
  237. if (not is_void(current_procdef.rettype.def)) then
  238. hs:=retstr
  239. else
  240. Message(asmr_e_void_function);
  241. end
  242. else if upper(hs)='__OLDEBP' then
  243. begin
  244. { complicate to check there }
  245. { we do it: }
  246. if current_procdef.parast.symtablelevel>normal_function_level then
  247. hs:=tostr(current_procinfo.framepointer_offset)+
  248. '('+gas_reg2str[framereg.enum]+')'
  249. else
  250. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  251. end;
  252. end;
  253. end;
  254. end;
  255. end;
  256. s:=s+hs;
  257. end;
  258. end;
  259. '{',';',#10,#13 : begin
  260. if pos(retstr,s) > 0 then
  261. tvarsym(current_procdef.funcretsym).varstate:=vs_assigned;
  262. writeasmline;
  263. c:=current_scanner.asmgetchar;
  264. end;
  265. #26 : Message(scan_f_end_of_file);
  266. else
  267. begin
  268. current_scanner.gettokenpos;
  269. inc(byte(s[0]));
  270. s[length(s)]:=c;
  271. c:=current_scanner.asmgetchar;
  272. end;
  273. end;
  274. end;
  275. writeasmline;
  276. assemble:=casmnode.create(code);
  277. end;
  278. {*****************************************************************************
  279. Initialize
  280. *****************************************************************************}
  281. const
  282. {$ifdef x86_64}
  283. asmmode_x86_64_direct_info : tasmmodeinfo =
  284. (
  285. id : asmmode_direct;
  286. idtxt : 'DIRECT'
  287. );
  288. {$else x86_64}
  289. asmmode_i386_direct_info : tasmmodeinfo =
  290. (
  291. id : asmmode_direct;
  292. idtxt : 'DIRECT'
  293. );
  294. {$endif x86_64}
  295. initialization
  296. {$ifdef x86_64}
  297. RegisterAsmMode(asmmode_x86_64_direct_info);
  298. {$else x86_64}
  299. RegisterAsmMode(asmmode_i386_direct_info);
  300. {$endif x86_64}
  301. end.
  302. {
  303. $Log$
  304. Revision 1.3 2003-05-13 19:15:28 peter
  305. * removed radirect
  306. Revision 1.2 2003/05/01 07:59:43 florian
  307. * introduced defaultordconsttype to decribe the default size of ordinal constants
  308. on 64 bit CPUs it's equal to cs64bitdef while on 32 bit CPUs it's equal to s32bitdef
  309. + added defines CPU32 and CPU64 for 32 bit and 64 bit CPUs
  310. * int64s/qwords are allowed as for loop counter on 64 bit CPUs
  311. Revision 1.1 2003/04/30 15:45:35 florian
  312. * merged more x86-64/i386 code
  313. Revision 1.11 2003/04/27 11:21:36 peter
  314. * aktprocdef renamed to current_procdef
  315. * procinfo renamed to current_procinfo
  316. * procinfo will now be stored in current_module so it can be
  317. cleaned up properly
  318. * gen_main_procsym changed to create_main_proc and release_main_proc
  319. to also generate a tprocinfo structure
  320. * fixed unit implicit initfinal
  321. Revision 1.10 2003/04/27 07:29:52 peter
  322. * current_procdef cleanup, current_procdef is now always nil when parsing
  323. a new procdef declaration
  324. * aktprocsym removed
  325. * lexlevel removed, use symtable.symtablelevel instead
  326. * implicit init/final code uses the normal genentry/genexit
  327. * funcret state checking updated for new funcret handling
  328. Revision 1.9 2003/04/25 20:59:35 peter
  329. * removed funcretn,funcretsym, function result is now in varsym
  330. and aliases for result and function name are added using absolutesym
  331. * vs_hidden parameter for funcret passed in parameter
  332. * vs_hidden fixes
  333. * writenode changed to printnode and released from extdebug
  334. * -vp option added to generate a tree.log with the nodetree
  335. * nicer printnode for statements, callnode
  336. Revision 1.8 2003/04/25 12:04:31 florian
  337. * merged agx64att and ag386att to x86/agx86att
  338. Revision 1.7 2003/04/21 20:05:10 peter
  339. * removed some ie checks
  340. Revision 1.6 2003/01/08 18:43:57 daniel
  341. * Tregister changed into a record
  342. Revision 1.5 2002/11/25 17:43:27 peter
  343. * splitted defbase in defutil,symutil,defcmp
  344. * merged isconvertable and is_equal into compare_defs(_ext)
  345. * made operator search faster by walking the list only once
  346. Revision 1.4 2002/11/18 17:32:00 peter
  347. * pass proccalloption to ret_in_xxx and push_xxx functions
  348. Revision 1.3 2002/09/03 16:26:28 daniel
  349. * Make Tprocdef.defs protected
  350. Revision 1.2 2002/08/17 09:23:47 florian
  351. * first part of procinfo rewrite
  352. Revision 1.1 2002/08/10 14:47:50 carl
  353. + moved target_cpu_string to cpuinfo
  354. * renamed asmmode enum.
  355. * assembler reader has now less ifdef's
  356. * move from nppcmem.pas -> ncgmem.pas vec. node.
  357. Revision 1.21 2002/07/20 11:58:05 florian
  358. * types.pas renamed to defbase.pas because D6 contains a types
  359. unit so this would conflicts if D6 programms are compiled
  360. + Willamette/SSE2 instructions to assembler added
  361. Revision 1.20 2002/07/11 14:41:34 florian
  362. * start of the new generic parameter handling
  363. Revision 1.19 2002/07/01 18:46:34 peter
  364. * internal linker
  365. * reorganized aasm layer
  366. Revision 1.18 2002/05/18 13:34:26 peter
  367. * readded missing revisions
  368. Revision 1.17 2002/05/16 19:46:52 carl
  369. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  370. + try to fix temp allocation (still in ifdef)
  371. + generic constructor calls
  372. + start of tassembler / tmodulebase class cleanup
  373. Revision 1.15 2002/05/12 16:53:18 peter
  374. * moved entry and exitcode to ncgutil and cgobj
  375. * foreach gets extra argument for passing local data to the
  376. iterator function
  377. * -CR checks also class typecasts at runtime by changing them
  378. into as
  379. * fixed compiler to cycle with the -CR option
  380. * fixed stabs with elf writer, finally the global variables can
  381. be watched
  382. * removed a lot of routines from cga unit and replaced them by
  383. calls to cgobj
  384. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  385. u32bit then the other is typecasted also to u32bit without giving
  386. a rangecheck warning/error.
  387. * fixed pascal calling method with reversing also the high tree in
  388. the parast, detected by tcalcst3 test
  389. Revision 1.14 2002/04/15 19:12:09 carl
  390. + target_info.size_of_pointer -> pointer_size
  391. + some cleanup of unused types/variables
  392. * move several constants from cpubase to their specific units
  393. (where they are used)
  394. + att_Reg2str -> gas_reg2str
  395. + int_reg2str -> std_reg2str
  396. Revision 1.13 2002/04/14 17:01:52 carl
  397. + att_reg2str -> gas_reg2str
  398. }