radirect.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. Reads inline Alpha 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 Alpha 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. agaxpgas,
  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<>R_NO) then
  158. hs:=gas_reg2str[procinfo.framepointer]
  159. else
  160. hs:=tostr(tvarsym(sym).address)+
  161. '('+gas_reg2str[procinfo.framepointer]+')';
  162. end;
  163. end
  164. else
  165. { call to local function }
  166. if (sym.typ=procsym) and (pos('BL',upper(s))>0) then
  167. hs:=tprocsym(sym).first_procdef.mangledname;
  168. end
  169. else
  170. begin
  171. if assigned(aktprocdef.parast) then
  172. sym:=tsym(aktprocdef.parast.search(upper(hs)))
  173. else
  174. sym:=nil;
  175. if assigned(sym) then
  176. begin
  177. if sym.typ=varsym then
  178. begin
  179. l:=tvarsym(sym).address;
  180. { set offset }
  181. inc(l,aktprocdef.parast.address_fixup);
  182. hs:=tostr(l)+'('+gas_reg2str[procinfo.framepointer]+')';
  183. if pos(',',s) > 0 then
  184. tvarsym(sym).varstate:=vs_used;
  185. end;
  186. end
  187. { I added that but it creates a problem in line.ppi
  188. because there is a local label wbuffer and
  189. a static variable WBUFFER ...
  190. what would you decide, florian ?}
  191. else
  192. begin
  193. searchsym(upper(hs),sym,srsymtable);
  194. if assigned(sym) and (sym.owner.symtabletype in [globalsymtable,staticsymtable]) then
  195. begin
  196. case sym.typ of
  197. varsym :
  198. begin
  199. Message2(asmr_h_direct_global_to_mangled,hs,tvarsym(sym).mangledname);
  200. hs:=tvarsym(sym).mangledname;
  201. inc(tvarsym(sym).refs);
  202. end;
  203. typedconstsym :
  204. begin
  205. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  206. hs:=ttypedconstsym(sym).mangledname;
  207. end;
  208. procsym :
  209. begin
  210. { procs can be called or the address can be loaded }
  211. if (pos('BL',upper(s))>0) {or (pos('LEA',upper(s))>0))} then
  212. begin
  213. if Tprocsym(sym).procdef_count>1 then
  214. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  215. Message2(asmr_h_direct_global_to_mangled,hs,tprocsym(sym).first_procdef.mangledname);
  216. hs:=tprocsym(sym).first_procdef.mangledname;
  217. end;
  218. end;
  219. else
  220. Message(asmr_e_wrong_sym_type);
  221. end;
  222. end
  223. {$ifdef dummy}
  224. else if upper(hs)='__SELF' then
  225. begin
  226. if assigned(procinfo^._class) then
  227. hs:=tostr(procinfo^.selfpointer_offset)+
  228. '('+gas_reg2str[procinfo^.framepointer]+')'
  229. else
  230. Message(asmr_e_cannot_use_SELF_outside_a_method);
  231. end
  232. else if upper(hs)='__RESULT' then
  233. begin
  234. if (not is_void(aktprocdef.rettype.def)) then
  235. hs:=retstr
  236. else
  237. Message(asmr_e_void_function);
  238. end
  239. { implement old stack/frame pointer access for nested procedures }
  240. {!!!!
  241. else if upper(hs)='__OLDSP' then
  242. begin
  243. { complicate to check there }
  244. { we do it: }
  245. if lexlevel>normal_function_level then
  246. hs:=tostr(procinfo^.framepointer_offset)+
  247. '('+gas_reg2str[procinfo^.framepointer]+')'
  248. else
  249. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  250. end;
  251. }
  252. end;
  253. {$endif dummy}
  254. end;
  255. end;
  256. end;
  257. end;
  258. s:=s+hs;
  259. end;
  260. end;
  261. '{',';',#10,#13:
  262. begin
  263. if pos(retstr,s) > 0 then
  264. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  265. writeasmline;
  266. c:=current_scanner.asmgetchar;
  267. end;
  268. #26:
  269. Message(scan_f_end_of_file);
  270. else
  271. begin
  272. current_scanner.gettokenpos;
  273. inc(byte(s[0]));
  274. s[length(s)]:=c;
  275. c:=current_scanner.asmgetchar;
  276. end;
  277. end;
  278. end;
  279. writeasmline;
  280. assemble:=casmnode.create(code);
  281. end;
  282. {*****************************************************************************
  283. Initialize
  284. *****************************************************************************}
  285. const
  286. asmmode_ppc_direct_info : tasmmodeinfo =
  287. (
  288. id : asmmode_direct;
  289. idtxt : 'DIRECT'
  290. );
  291. initialization
  292. RegisterAsmMode(asmmode_ppc_direct_info);
  293. end.
  294. {
  295. $Log$
  296. Revision 1.1 2002-09-29 23:42:45 florian
  297. * several fixes to get forward with alpha compilation
  298. Revision 1.5 2002/09/03 19:04:18 daniel
  299. * Fixed PowerPC & M68000 compilation
  300. Revision 1.4 2002/09/03 16:26:28 daniel
  301. * Make Tprocdef.defs protected
  302. Revision 1.3 2002/08/31 15:59:31 florian
  303. + HEAP* stuff must be generated for Linux/PPC as well
  304. + direct assembler reader searches now global and static symtables as well
  305. Revision 1.2 2002/08/18 21:36:42 florian
  306. + handling of local variables in direct reader implemented
  307. Revision 1.1 2002/08/10 14:52:52 carl
  308. + moved target_cpu_string to cpuinfo
  309. * renamed asmmode enum.
  310. * assembler reader has now less ifdef's
  311. * move from nppcmem.pas -> ncgmem.pas vec. node.
  312. Revision 1.2 2002/07/28 20:45:23 florian
  313. + added direct assembler reader for PowerPC
  314. Revision 1.1 2002/07/11 14:41:34 florian
  315. * start of the new generic parameter handling
  316. }