radirect.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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,defbase,
  38. { pass 1 }
  39. nbas,
  40. { parser }
  41. scanner,
  42. { codegen }
  43. cgbase,
  44. { constants }
  45. agppcgas
  46. ;
  47. function assemble : tnode;
  48. var
  49. retstr,s,hs : string;
  50. c : char;
  51. ende : boolean;
  52. srsym,sym : tsym;
  53. srsymtable : tsymtable;
  54. code : TAAsmoutput;
  55. i,l : longint;
  56. procedure writeasmline;
  57. var
  58. i : longint;
  59. begin
  60. i:=length(s);
  61. while (i>0) and (s[i] in [' ',#9]) do
  62. dec(i);
  63. s[0]:=chr(i);
  64. if s<>'' then
  65. code.concat(Tai_direct.Create(strpnew(s)));
  66. { consider it set function set if the offset was loaded }
  67. if assigned(aktprocdef.funcretsym) and
  68. (pos(retstr,upper(s))>0) then
  69. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  70. s:='';
  71. end;
  72. begin
  73. ende:=false;
  74. s:='';
  75. if assigned(aktprocdef.funcretsym) and
  76. is_fpu(aktprocdef.rettype.def) then
  77. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  78. { !!!!!
  79. if (not is_void(aktprocdef.rettype.def)) then
  80. retstr:=upper(tostr(procinfo^.return_offset)+'('+gas_reg2str[procinfo^.framepointer]+')')
  81. else
  82. }
  83. retstr:='';
  84. c:=current_scanner.asmgetchar;
  85. code:=TAAsmoutput.Create;
  86. while not(ende) do
  87. begin
  88. { wrong placement
  89. current_scanner.gettokenpos; }
  90. case c of
  91. 'A'..'Z','a'..'z','_':
  92. begin
  93. current_scanner.gettokenpos;
  94. i:=0;
  95. hs:='';
  96. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  97. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  98. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  99. or (c='_') do
  100. begin
  101. inc(i);
  102. hs[i]:=c;
  103. c:=current_scanner.asmgetchar;
  104. end;
  105. hs[0]:=chr(i);
  106. if upper(hs)='END' then
  107. ende:=true
  108. else
  109. begin
  110. if c=':' then
  111. begin
  112. searchsym(upper(hs),srsym,srsymtable);
  113. if srsym<>nil then
  114. if (srsym.typ = labelsym) then
  115. Begin
  116. hs:=tlabelsym(srsym).lab.name;
  117. tlabelsym(srsym).lab.is_set:=true;
  118. end
  119. else
  120. Message(asmr_w_using_defined_as_local);
  121. end;
  122. {$ifdef dummy}
  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. if (s[length(s)]='%') and
  132. ret_in_acc(aktprocdef.rettype.def) and
  133. ((pos('AX',upper(hs))>0) or
  134. (pos('AL',upper(hs))>0)) then
  135. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  136. if (s[length(s)]<>'%') and
  137. (s[length(s)]<>'$') and
  138. ((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. {variables set are after a comma }
  154. {like in movl %eax,I }
  155. if pos(',',s) > 0 then
  156. tvarsym(sym).varstate:=vs_used
  157. else
  158. if (pos('MOV',upper(s)) > 0) and (tvarsym(sym).varstate=vs_declared) then
  159. Message1(sym_n_uninitialized_local_variable,hs);
  160. if (vo_is_external in tvarsym(sym).varoptions) then
  161. hs:=tvarsym(sym).mangledname
  162. else
  163. hs:='-'+tostr(tvarsym(sym).address)+
  164. '('+gas_reg2str[procinfo^.framepointer]+')';
  165. end
  166. else
  167. { call to local function }
  168. if (sym.typ=procsym) and ((pos('CALL',upper(s))>0) or
  169. (pos('LEA',upper(s))>0)) then
  170. begin
  171. hs:=tprocsym(sym).defs^.def.mangledname;
  172. end;
  173. end
  174. else
  175. begin
  176. if assigned(aktprocdef.parast) then
  177. sym:=tsym(aktprocdef.parast.search(upper(hs)))
  178. else
  179. sym:=nil;
  180. if assigned(sym) then
  181. begin
  182. if sym.typ=varsym then
  183. begin
  184. l:=tvarsym(sym).address;
  185. { set offset }
  186. inc(l,aktprocdef.parast.address_fixup);
  187. hs:=tostr(l)+'('+gas_reg2str[procinfo^.framepointer]+')';
  188. if pos(',',s) > 0 then
  189. tvarsym(sym).varstate:=vs_used;
  190. end;
  191. end
  192. { I added that but it creates a problem in line.ppi
  193. because there is a local label wbuffer and
  194. a static variable WBUFFER ...
  195. what would you decide, florian ?}
  196. else
  197. begin
  198. searchsym(upper(hs),sym,srsymtable);
  199. if assigned(sym) and (sym.owner.symtabletype in [globalsymtable,staticsymtable]) then
  200. begin
  201. case sym.typ of
  202. varsym :
  203. begin
  204. Message2(asmr_h_direct_global_to_mangled,hs,tvarsym(sym).mangledname);
  205. hs:=tvarsym(sym).mangledname;
  206. inc(tvarsym(sym).refs);
  207. end;
  208. typedconstsym :
  209. begin
  210. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  211. hs:=ttypedconstsym(sym).mangledname;
  212. end;
  213. procsym :
  214. begin
  215. { procs can be called or the address can be loaded }
  216. if ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  217. begin
  218. if assigned(tprocsym(sym).defs^.def) then
  219. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  220. Message2(asmr_h_direct_global_to_mangled,hs,tprocsym(sym).defs^.def.mangledname);
  221. hs:=tprocsym(sym).defs^.def.mangledname;
  222. end;
  223. end;
  224. else
  225. Message(asmr_e_wrong_sym_type);
  226. end;
  227. end
  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. end;
  258. end;
  259. end;
  260. {$endif dummy}
  261. s:=s+hs;
  262. end;
  263. end;
  264. '{',';',#10,#13:
  265. begin
  266. if pos(retstr,s) > 0 then
  267. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  268. writeasmline;
  269. c:=current_scanner.asmgetchar;
  270. end;
  271. #26:
  272. Message(scan_f_end_of_file);
  273. else
  274. begin
  275. current_scanner.gettokenpos;
  276. inc(byte(s[0]));
  277. s[length(s)]:=c;
  278. c:=current_scanner.asmgetchar;
  279. end;
  280. end;
  281. end;
  282. writeasmline;
  283. assemble:=casmnode.create(code);
  284. end;
  285. {*****************************************************************************
  286. Initialize
  287. *****************************************************************************}
  288. const
  289. asmmode_ppc_direct_info : tasmmodeinfo =
  290. (
  291. id : asmmode_direct;
  292. idtxt : 'DIRECT'
  293. );
  294. initialization
  295. RegisterAsmMode(asmmode_ppc_direct_info);
  296. end.
  297. {
  298. $Log$
  299. Revision 1.1 2002-08-10 14:52:52 carl
  300. + moved target_cpu_string to cpuinfo
  301. * renamed asmmode enum.
  302. * assembler reader has now less ifdef's
  303. * move from nppcmem.pas -> ncgmem.pas vec. node.
  304. Revision 1.2 2002/07/28 20:45:23 florian
  305. + added direct assembler reader for PowerPC
  306. Revision 1.1 2002/07/11 14:41:34 florian
  307. * start of the new generic parameter handling
  308. }