radirect.pas 18 KB

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