2
0

radirect.pas 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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,procinfo,
  44. { constants }
  45. 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).localloc.loc=LOC_REGISTER) then
  166. hs:=gas_regname(tvarsym(sym).localloc.register)
  167. else
  168. hs:='%%'+tvarsym(sym).name;
  169. end;
  170. end
  171. else
  172. { call to local function }
  173. if (sym.typ=procsym) and (pos('BL',upper(s))>0) then
  174. hs:=tprocsym(sym).first_procdef.mangledname;
  175. end
  176. else
  177. begin
  178. if assigned(current_procinfo.procdef.parast) then
  179. sym:=tsym(current_procinfo.procdef.parast.search(upper(hs)))
  180. else
  181. sym:=nil;
  182. if assigned(sym) then
  183. begin
  184. if sym.typ=varsym then
  185. begin
  186. hs:='%%'+tvarsym(sym).name;
  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. constsym :
  202. begin
  203. inc(tconstsym(sym).refs);
  204. case tconstsym(sym).consttyp of
  205. constint,constchar,constbool :
  206. hs:=tostr(tconstsym(sym).value.valueord);
  207. constpointer :
  208. hs:=tostr(tconstsym(sym).value.valueordptr);
  209. else
  210. Message(asmr_e_wrong_sym_type);
  211. end;
  212. end;
  213. varsym :
  214. begin
  215. Message2(asmr_h_direct_global_to_mangled,hs,tvarsym(sym).mangledname);
  216. hs:=tvarsym(sym).mangledname;
  217. inc(tvarsym(sym).refs);
  218. end;
  219. typedconstsym :
  220. begin
  221. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  222. hs:=ttypedconstsym(sym).mangledname;
  223. end;
  224. procsym :
  225. begin
  226. { procs can be called or the address can be loaded }
  227. if (pos('BL',upper(s))>0) {or (pos('LEA',upper(s))>0))} then
  228. begin
  229. if Tprocsym(sym).procdef_count>1 then
  230. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  231. Message2(asmr_h_direct_global_to_mangled,hs,tprocsym(sym).first_procdef.mangledname);
  232. hs:=tprocsym(sym).first_procdef.mangledname;
  233. end;
  234. end;
  235. else
  236. Message(asmr_e_wrong_sym_type);
  237. end;
  238. end
  239. {$ifdef dummy}
  240. else if upper(hs)='__SELF' then
  241. begin
  242. if assigned(procinfo^._class) then
  243. hs:=tostr(procinfo^.selfpointer_offset)+
  244. '('+gas_reg2str[procinfo^.framepointer]+')'
  245. else
  246. Message(asmr_e_cannot_use_SELF_outside_a_method);
  247. end
  248. else if upper(hs)='__RESULT' then
  249. begin
  250. if (not is_void(current_procinfo.procdef.rettype.def)) then
  251. hs:=retstr
  252. else
  253. Message(asmr_e_void_function);
  254. end
  255. { implement old stack/frame pointer access for nested procedures }
  256. (* !!!!
  257. else if upper(hs)='__OLDSP' then
  258. begin
  259. { complicate to check there }
  260. { we do it: }
  261. if lexlevel>normal_function_level then
  262. hs:=tostr(procinfo^.framepointer_offset)+
  263. '('+gas_reg2str[procinfo^.framepointer]+')'
  264. else
  265. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  266. end;
  267. *)
  268. end;
  269. {$endif dummy}
  270. end;
  271. end;
  272. end;
  273. end;
  274. s:=s+hs;
  275. end;
  276. end;
  277. '{',';',#10,#13:
  278. begin
  279. if pos(retstr,s) > 0 then
  280. tvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_assigned;
  281. writeasmline;
  282. c:=current_scanner.asmgetchar;
  283. end;
  284. #26:
  285. Message(scan_f_end_of_file);
  286. else
  287. begin
  288. current_scanner.gettokenpos;
  289. inc(byte(s[0]));
  290. s[length(s)]:=c;
  291. c:=current_scanner.asmgetchar;
  292. end;
  293. end;
  294. end;
  295. writeasmline;
  296. assemble:=casmnode.create(code);
  297. end;
  298. {*****************************************************************************
  299. Initialize
  300. *****************************************************************************}
  301. const
  302. asmmode_ppc_direct_info : tasmmodeinfo =
  303. (
  304. id : asmmode_direct;
  305. idtxt : 'DIRECT'
  306. );
  307. initialization
  308. RegisterAsmMode(asmmode_ppc_direct_info);
  309. end.
  310. {
  311. $Log$
  312. Revision 1.19 2003-10-01 20:34:49 peter
  313. * procinfo unit contains tprocinfo
  314. * cginfo renamed to cgbase
  315. * moved cgmessage to verbose
  316. * fixed ppc and sparc compiles
  317. Revision 1.18 2003/09/04 00:15:29 florian
  318. * first bunch of adaptions of arm compiler for new register type
  319. Revision 1.17 2003/09/03 19:35:24 peter
  320. * powerpc compiles again
  321. Revision 1.16 2003/06/13 21:19:32 peter
  322. * current_procdef removed, use current_procinfo.procdef instead
  323. Revision 1.15 2003/06/02 21:42:05 jonas
  324. * function results can now also be regvars
  325. - removed tprocinfo.return_offset, never use it again since it's invalid
  326. if the result is a regvar
  327. Revision 1.14 2003/05/30 23:57:08 peter
  328. * more sparc cleanup
  329. * accumulator removed, splitted in function_return_reg (called) and
  330. function_result_reg (caller)
  331. Revision 1.13 2003/04/27 11:21:36 peter
  332. * aktprocdef renamed to current_procinfo.procdef
  333. * procinfo renamed to current_procinfo
  334. * procinfo will now be stored in current_module so it can be
  335. cleaned up properly
  336. * gen_main_procsym changed to create_main_proc and release_main_proc
  337. to also generate a tprocinfo structure
  338. * fixed unit implicit initfinal
  339. Revision 1.12 2003/04/27 07:48:05 peter
  340. * updated for removed lexlevel
  341. Revision 1.11 2003/04/25 21:05:22 florian
  342. * fixed tfuncretsym stuff in powerpc specific part
  343. Revision 1.10 2003/04/24 12:05:53 florian
  344. * symbols which are register identifiers aren't resolved anymore
  345. Revision 1.9 2003/04/23 22:18:01 peter
  346. * fixes to get rtl compiled
  347. Revision 1.8 2003/03/22 18:00:27 jonas
  348. * fixes for new regallocator
  349. Revision 1.7 2003/01/08 18:43:58 daniel
  350. * Tregister changed into a record
  351. Revision 1.6 2002/11/25 17:43:28 peter
  352. * splitted defbase in defutil,symutil,defcmp
  353. * merged isconvertable and is_equal into compare_defs(_ext)
  354. * made operator search faster by walking the list only once
  355. Revision 1.5 2002/09/03 19:04:18 daniel
  356. * Fixed PowerPC & M68000 compilation
  357. Revision 1.4 2002/09/03 16:26:28 daniel
  358. * Make Tprocdef.defs protected
  359. Revision 1.3 2002/08/31 15:59:31 florian
  360. + HEAP* stuff must be generated for Linux/PPC as well
  361. + direct assembler reader searches now global and static symtables as well
  362. Revision 1.2 2002/08/18 21:36:42 florian
  363. + handling of local variables in direct reader implemented
  364. Revision 1.1 2002/08/10 14:52:52 carl
  365. + moved target_cpu_string to cpuinfo
  366. * renamed asmmode enum.
  367. * assembler reader has now less ifdef's
  368. * move from nppcmem.pas -> ncgmem.pas vec. node.
  369. Revision 1.2 2002/07/28 20:45:23 florian
  370. + added direct assembler reader for PowerPC
  371. Revision 1.1 2002/07/11 14:41:34 florian
  372. * start of the new generic parameter handling
  373. }