radirect.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. Reads inline Alpha assembler and writes the lines direct to the output
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {
  18. This unit reads Alpha inline assembler and writes the lines direct to the output file.
  19. }
  20. unit radirect;
  21. {$i fpcdefs.inc}
  22. interface
  23. uses
  24. node;
  25. function assemble : tnode;
  26. implementation
  27. uses
  28. { common }
  29. cutils,
  30. { global }
  31. globals,verbose,
  32. systems,
  33. { aasm }
  34. aasmbase,aasmtai,aasmdata,aasmcpu,
  35. { symtable }
  36. symconst,symbase,symtype,symsym,symtable,defbase,
  37. { pass 1 }
  38. nbas,
  39. { parser }
  40. scanner,
  41. { codegen }
  42. cgbase,
  43. { constants }
  44. agaxpgas,
  45. cpubase
  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 : TAsmList;
  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_initialised;
  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_initialised;
  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:=TAsmList.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. else
  123. { access to local variables }
  124. if assigned(aktprocdef) then
  125. begin
  126. { I don't know yet, what the ppc port requires }
  127. { we'll see how things settle down }
  128. { is the last written character an special }
  129. { char ? }
  130. { !!!
  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_initialised;
  136. }
  137. if ((s[length(s)]<>'0') or (hs[1]<>'x')) then
  138. begin
  139. if assigned(aktprocdef.localst) and
  140. (lexlevel >= normal_function_level) then
  141. sym:=tsym(aktprocdef.localst.search(upper(hs)))
  142. else
  143. sym:=nil;
  144. if assigned(sym) then
  145. begin
  146. if (sym.typ=labelsym) then
  147. Begin
  148. hs:=tlabelsym(sym).lab.name;
  149. end
  150. else if sym.typ=varsym then
  151. begin
  152. if (vo_is_external in tvarsym(sym).varoptions) then
  153. hs:=tvarsym(sym).mangledname
  154. else
  155. begin
  156. if (tvarsym(sym).reg<>R_NO) then
  157. hs:=gas_reg2str[procinfo.framepointer]
  158. else
  159. hs:=tostr(tvarsym(sym).address)+
  160. '('+gas_reg2str[procinfo.framepointer]+')';
  161. end;
  162. end
  163. else
  164. { call to local function }
  165. if (sym.typ=procsym) and (pos('BL',upper(s))>0) then
  166. hs:=tprocsym(sym).first_procdef.mangledname;
  167. end
  168. else
  169. begin
  170. if assigned(aktprocdef.parast) then
  171. sym:=tsym(aktprocdef.parast.search(upper(hs)))
  172. else
  173. sym:=nil;
  174. if assigned(sym) then
  175. begin
  176. if sym.typ=varsym then
  177. begin
  178. l:=tvarsym(sym).address;
  179. { set offset }
  180. inc(l,aktprocdef.parast.address_fixup);
  181. hs:=tostr(l)+'('+gas_reg2str[procinfo.framepointer]+')';
  182. if pos(',',s) > 0 then
  183. tvarsym(sym).varstate:=vs_readwritten;
  184. end;
  185. end
  186. { I added that but it creates a problem in line.ppi
  187. because there is a local label wbuffer and
  188. a static variable WBUFFER ...
  189. what would you decide, florian ?}
  190. else
  191. begin
  192. searchsym(upper(hs),sym,srsymtable);
  193. if assigned(sym) and (sym.owner.symtabletype in [globalsymtable,staticsymtable]) then
  194. begin
  195. case sym.typ of
  196. varsym :
  197. begin
  198. Message2(asmr_h_direct_global_to_mangled,hs,tvarsym(sym).mangledname);
  199. hs:=tvarsym(sym).mangledname;
  200. inc(tvarsym(sym).refs);
  201. end;
  202. typedconstsym :
  203. begin
  204. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  205. hs:=ttypedconstsym(sym).mangledname;
  206. end;
  207. procsym :
  208. begin
  209. { procs can be called or the address can be loaded }
  210. if (pos('BL',upper(s))>0) {or (pos('LEA',upper(s))>0))} then
  211. begin
  212. if Tprocsym(sym).procdef_count>1 then
  213. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  214. Message2(asmr_h_direct_global_to_mangled,hs,tprocsym(sym).first_procdef.mangledname);
  215. hs:=tprocsym(sym).first_procdef.mangledname;
  216. end;
  217. end;
  218. else
  219. Message(asmr_e_wrong_sym_type);
  220. end;
  221. end
  222. {$ifdef dummy}
  223. else if upper(hs)='__SELF' then
  224. begin
  225. if assigned(procinfo^._class) then
  226. hs:=tostr(procinfo^.selfpointer_offset)+
  227. '('+gas_reg2str[procinfo^.framepointer]+')'
  228. else
  229. Message(asmr_e_cannot_use_SELF_outside_a_method);
  230. end
  231. else if upper(hs)='__RESULT' then
  232. begin
  233. if (not is_void(aktprocdef.rettype.def)) then
  234. hs:=retstr
  235. else
  236. Message(asmr_e_void_function);
  237. end
  238. { implement old stack/frame pointer access for nested procedures }
  239. {!!!!
  240. else if upper(hs)='__OLDSP' then
  241. begin
  242. { complicate to check there }
  243. { we do it: }
  244. if lexlevel>normal_function_level then
  245. hs:=tostr(procinfo^.framepointer_offset)+
  246. '('+gas_reg2str[procinfo^.framepointer]+')'
  247. else
  248. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  249. end;
  250. }
  251. end;
  252. {$endif dummy}
  253. end;
  254. end;
  255. end;
  256. end;
  257. s:=s+hs;
  258. end;
  259. end;
  260. '{',';',#10,#13:
  261. begin
  262. if pos(retstr,s) > 0 then
  263. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_initialised;
  264. writeasmline;
  265. c:=current_scanner.asmgetchar;
  266. end;
  267. #26:
  268. Message(scan_f_end_of_file);
  269. else
  270. begin
  271. current_scanner.gettokenpos;
  272. inc(byte(s[0]));
  273. s[length(s)]:=c;
  274. c:=current_scanner.asmgetchar;
  275. end;
  276. end;
  277. end;
  278. writeasmline;
  279. assemble:=casmnode.create(code);
  280. end;
  281. {*****************************************************************************
  282. Initialize
  283. *****************************************************************************}
  284. const
  285. asmmode_ppc_direct_info : tasmmodeinfo =
  286. (
  287. id : asmmode_direct;
  288. idtxt : 'DIRECT'
  289. );
  290. initialization
  291. RegisterAsmMode(asmmode_ppc_direct_info);
  292. end.