radirect.pas 20 KB

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