radirect.pas 19 KB

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