radirect.pas 20 KB

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