radirect.pas 16 KB

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