radirect.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Reads inline Powerpc 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. {
  19. This unit reads PowerPC inline assembler and writes the lines direct to the output file.
  20. }
  21. unit radirect;
  22. {$i fpcdefs.inc}
  23. interface
  24. uses
  25. node;
  26. function assemble : tnode;
  27. implementation
  28. uses
  29. { common }
  30. cutils,
  31. { global }
  32. globals,verbose,
  33. systems,
  34. { aasm }
  35. aasmbase,aasmtai,aasmcpu,
  36. { symtable }
  37. symconst,symbase,symtype,symsym,symtable,defutil,
  38. { pass 1 }
  39. nbas,
  40. { parser }
  41. scanner,
  42. { codegen }
  43. cgbase,
  44. { constants }
  45. agppcgas,
  46. cpubase
  47. ;
  48. { checks if a string identifies a register }
  49. { it should be optimized }
  50. function is_register(const s : string) : boolean;
  51. var
  52. r : toldregister;
  53. begin
  54. is_register:=false;
  55. if length(s)>5 then
  56. exit;
  57. for r:=low(gas_reg2str) to high(gas_reg2str) do
  58. if gas_reg2str[r]=s then
  59. begin
  60. is_register:=true;
  61. exit;
  62. end;
  63. end;
  64. function assemble : tnode;
  65. var
  66. retstr,s,hs : string;
  67. c : char;
  68. ende : boolean;
  69. srsym,sym : tsym;
  70. srsymtable : tsymtable;
  71. code : TAAsmoutput;
  72. framereg : tregister;
  73. i,l : longint;
  74. procedure writeasmline;
  75. var
  76. i : longint;
  77. begin
  78. i:=length(s);
  79. while (i>0) and (s[i] in [' ',#9]) do
  80. dec(i);
  81. s[0]:=chr(i);
  82. if s<>'' then
  83. code.concat(Tai_direct.Create(strpnew(s)));
  84. { consider it set function set if the offset was loaded }
  85. if assigned(current_procinfo.procdef.funcretsym) and
  86. (pos(retstr,upper(s))>0) then
  87. tvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_assigned;
  88. s:='';
  89. end;
  90. begin
  91. ende:=false;
  92. framereg.enum:=R_INTREGISTER;
  93. framereg.number:=NR_STACK_POINTER_REG;
  94. convert_register_to_enum(framereg);
  95. s:='';
  96. if assigned(current_procinfo.procdef.funcretsym) and
  97. is_fpu(current_procinfo.procdef.rettype.def) then
  98. tvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_assigned;
  99. { !!!!!
  100. if (not is_void(current_procinfo.procdef.rettype.def)) then
  101. retstr:=upper(tostr(tvarsym(current_procinfo.procdef.funcretsym).adjusted_address)+'('+gas_reg2str[procinfo^.framepointer]+')')
  102. else
  103. }
  104. retstr:='';
  105. c:=current_scanner.asmgetchar;
  106. code:=TAAsmoutput.Create;
  107. while not(ende) do
  108. begin
  109. { wrong placement
  110. current_scanner.gettokenpos; }
  111. case c of
  112. 'A'..'Z','a'..'z','_':
  113. begin
  114. current_scanner.gettokenpos;
  115. i:=0;
  116. hs:='';
  117. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  118. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  119. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  120. or (c='_') do
  121. begin
  122. inc(i);
  123. hs[i]:=c;
  124. c:=current_scanner.asmgetchar;
  125. end;
  126. hs[0]:=chr(i);
  127. if upper(hs)='END' then
  128. ende:=true
  129. else
  130. begin
  131. if c=':' then
  132. begin
  133. searchsym(upper(hs),srsym,srsymtable);
  134. if srsym<>nil then
  135. if (srsym.typ = labelsym) then
  136. Begin
  137. hs:=tlabelsym(srsym).lab.name;
  138. tlabelsym(srsym).lab.is_set:=true;
  139. end
  140. else
  141. Message(asmr_w_using_defined_as_local);
  142. end
  143. else
  144. { access to local variables }
  145. if assigned(current_procinfo.procdef) then
  146. begin
  147. { I don't know yet, what the ppc port requires }
  148. { we'll see how things settle down }
  149. { is the last written character an special }
  150. { char ? }
  151. { !!!
  152. if (s[length(s)]='%') and
  153. ret_in_acc(current_procinfo.procdef.rettype.def) and
  154. ((pos('AX',upper(hs))>0) or
  155. (pos('AL',upper(hs))>0)) then
  156. tfuncretsym(current_procinfo.procdef.funcretsym).funcretstate:=vs_assigned;
  157. }
  158. if ((s[length(s)]<>'0') or (hs[1]<>'x')) and not(is_register(hs)) then
  159. begin
  160. if assigned(current_procinfo.procdef.localst) and
  161. (current_procinfo.procdef.localst.symtablelevel >= normal_function_level) then
  162. sym:=tsym(current_procinfo.procdef.localst.search(upper(hs)))
  163. else
  164. sym:=nil;
  165. if assigned(sym) then
  166. begin
  167. if (sym.typ=labelsym) then
  168. Begin
  169. hs:=tlabelsym(sym).lab.name;
  170. end
  171. else if sym.typ=varsym then
  172. begin
  173. if (vo_is_external in tvarsym(sym).varoptions) then
  174. hs:=tvarsym(sym).mangledname
  175. else
  176. begin
  177. if (tvarsym(sym).reg.enum<>R_NO) then
  178. // until new regallocator stuff settles down
  179. // hs:=gas_reg2str[procinfo.framepointer.enum]
  180. hs:=gas_reg2str[framereg.enum]
  181. else
  182. hs:=tostr(tvarsym(sym).address)+
  183. // '('+gas_reg2str[procinfo.framepointer.enum]+')';
  184. '('+gas_reg2str[framereg.enum]+')';
  185. end;
  186. end
  187. else
  188. { call to local function }
  189. if (sym.typ=procsym) and (pos('BL',upper(s))>0) then
  190. hs:=tprocsym(sym).first_procdef.mangledname;
  191. end
  192. else
  193. begin
  194. if assigned(current_procinfo.procdef.parast) then
  195. sym:=tsym(current_procinfo.procdef.parast.search(upper(hs)))
  196. else
  197. sym:=nil;
  198. if assigned(sym) then
  199. begin
  200. if sym.typ=varsym then
  201. begin
  202. l:=tvarsym(sym).address;
  203. { set offset }
  204. inc(l,current_procinfo.procdef.parast.address_fixup);
  205. // hs:=tostr(l)+'('+gas_reg2str[procinfo.framepointer.enum]+')';
  206. hs:=tostr(l)+'('+gas_reg2str[framereg.enum]+')';
  207. if pos(',',s) > 0 then
  208. tvarsym(sym).varstate:=vs_used;
  209. end;
  210. end
  211. { I added that but it creates a problem in line.ppi
  212. because there is a local label wbuffer and
  213. a static variable WBUFFER ...
  214. what would you decide, florian ?}
  215. else
  216. begin
  217. searchsym(upper(hs),sym,srsymtable);
  218. if assigned(sym) and (sym.owner.symtabletype in [globalsymtable,staticsymtable]) then
  219. begin
  220. case sym.typ of
  221. constsym :
  222. begin
  223. inc(tconstsym(sym).refs);
  224. case tconstsym(sym).consttyp of
  225. constint,constchar,constbool :
  226. hs:=tostr(tconstsym(sym).value.valueord);
  227. constpointer :
  228. hs:=tostr(tconstsym(sym).value.valueordptr);
  229. else
  230. Message(asmr_e_wrong_sym_type);
  231. end;
  232. end;
  233. varsym :
  234. begin
  235. Message2(asmr_h_direct_global_to_mangled,hs,tvarsym(sym).mangledname);
  236. hs:=tvarsym(sym).mangledname;
  237. inc(tvarsym(sym).refs);
  238. end;
  239. typedconstsym :
  240. begin
  241. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  242. hs:=ttypedconstsym(sym).mangledname;
  243. end;
  244. procsym :
  245. begin
  246. { procs can be called or the address can be loaded }
  247. if (pos('BL',upper(s))>0) {or (pos('LEA',upper(s))>0))} then
  248. begin
  249. if Tprocsym(sym).procdef_count>1 then
  250. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  251. Message2(asmr_h_direct_global_to_mangled,hs,tprocsym(sym).first_procdef.mangledname);
  252. hs:=tprocsym(sym).first_procdef.mangledname;
  253. end;
  254. end;
  255. else
  256. Message(asmr_e_wrong_sym_type);
  257. end;
  258. end
  259. {$ifdef dummy}
  260. else if upper(hs)='__SELF' then
  261. begin
  262. if assigned(procinfo^._class) then
  263. hs:=tostr(procinfo^.selfpointer_offset)+
  264. '('+gas_reg2str[procinfo^.framepointer]+')'
  265. else
  266. Message(asmr_e_cannot_use_SELF_outside_a_method);
  267. end
  268. else if upper(hs)='__RESULT' then
  269. begin
  270. if (not is_void(current_procinfo.procdef.rettype.def)) then
  271. hs:=retstr
  272. else
  273. Message(asmr_e_void_function);
  274. end
  275. { implement old stack/frame pointer access for nested procedures }
  276. {!!!!
  277. else if upper(hs)='__OLDSP' then
  278. begin
  279. { complicate to check there }
  280. { we do it: }
  281. if lexlevel>normal_function_level then
  282. hs:=tostr(procinfo^.framepointer_offset)+
  283. '('+gas_reg2str[procinfo^.framepointer]+')'
  284. else
  285. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  286. end;
  287. }
  288. end;
  289. {$endif dummy}
  290. end;
  291. end;
  292. end;
  293. end;
  294. s:=s+hs;
  295. end;
  296. end;
  297. '{',';',#10,#13:
  298. begin
  299. if pos(retstr,s) > 0 then
  300. tvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_assigned;
  301. writeasmline;
  302. c:=current_scanner.asmgetchar;
  303. end;
  304. #26:
  305. Message(scan_f_end_of_file);
  306. else
  307. begin
  308. current_scanner.gettokenpos;
  309. inc(byte(s[0]));
  310. s[length(s)]:=c;
  311. c:=current_scanner.asmgetchar;
  312. end;
  313. end;
  314. end;
  315. writeasmline;
  316. assemble:=casmnode.create(code);
  317. end;
  318. {*****************************************************************************
  319. Initialize
  320. *****************************************************************************}
  321. const
  322. asmmode_ppc_direct_info : tasmmodeinfo =
  323. (
  324. id : asmmode_direct;
  325. idtxt : 'DIRECT'
  326. );
  327. initialization
  328. RegisterAsmMode(asmmode_ppc_direct_info);
  329. end.
  330. {
  331. $Log$
  332. Revision 1.16 2003-06-13 21:19:32 peter
  333. * current_procdef removed, use current_procinfo.procdef instead
  334. Revision 1.15 2003/06/02 21:42:05 jonas
  335. * function results can now also be regvars
  336. - removed tprocinfo.return_offset, never use it again since it's invalid
  337. if the result is a regvar
  338. Revision 1.14 2003/05/30 23:57:08 peter
  339. * more sparc cleanup
  340. * accumulator removed, splitted in function_return_reg (called) and
  341. function_result_reg (caller)
  342. Revision 1.13 2003/04/27 11:21:36 peter
  343. * aktprocdef renamed to current_procinfo.procdef
  344. * procinfo renamed to current_procinfo
  345. * procinfo will now be stored in current_module so it can be
  346. cleaned up properly
  347. * gen_main_procsym changed to create_main_proc and release_main_proc
  348. to also generate a tprocinfo structure
  349. * fixed unit implicit initfinal
  350. Revision 1.12 2003/04/27 07:48:05 peter
  351. * updated for removed lexlevel
  352. Revision 1.11 2003/04/25 21:05:22 florian
  353. * fixed tfuncretsym stuff in powerpc specific part
  354. Revision 1.10 2003/04/24 12:05:53 florian
  355. * symbols which are register identifiers aren't resolved anymore
  356. Revision 1.9 2003/04/23 22:18:01 peter
  357. * fixes to get rtl compiled
  358. Revision 1.8 2003/03/22 18:00:27 jonas
  359. * fixes for new regallocator
  360. Revision 1.7 2003/01/08 18:43:58 daniel
  361. * Tregister changed into a record
  362. Revision 1.6 2002/11/25 17:43:28 peter
  363. * splitted defbase in defutil,symutil,defcmp
  364. * merged isconvertable and is_equal into compare_defs(_ext)
  365. * made operator search faster by walking the list only once
  366. Revision 1.5 2002/09/03 19:04:18 daniel
  367. * Fixed PowerPC & M68000 compilation
  368. Revision 1.4 2002/09/03 16:26:28 daniel
  369. * Make Tprocdef.defs protected
  370. Revision 1.3 2002/08/31 15:59:31 florian
  371. + HEAP* stuff must be generated for Linux/PPC as well
  372. + direct assembler reader searches now global and static symtables as well
  373. Revision 1.2 2002/08/18 21:36:42 florian
  374. + handling of local variables in direct reader implemented
  375. Revision 1.1 2002/08/10 14:52:52 carl
  376. + moved target_cpu_string to cpuinfo
  377. * renamed asmmode enum.
  378. * assembler reader has now less ifdef's
  379. * move from nppcmem.pas -> ncgmem.pas vec. node.
  380. Revision 1.2 2002/07/28 20:45:23 florian
  381. + added direct assembler reader for PowerPC
  382. Revision 1.1 2002/07/11 14:41:34 florian
  383. * start of the new generic parameter handling
  384. }