radirect.pas 19 KB

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