radirect.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 ARM 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. cginfo,
  44. cgbase,
  45. { constants }
  46. itarmgas,
  47. cpubase
  48. ;
  49. function is_register(const s:string):boolean;
  50. begin
  51. is_register:=false;
  52. if gas_regnum_search(lower(s))<>NR_NO then
  53. is_register:=true;
  54. end;
  55. function assemble : tnode;
  56. var
  57. retstr,s,hs : string;
  58. c : char;
  59. ende : boolean;
  60. srsym,sym : tsym;
  61. srsymtable : tsymtable;
  62. code : TAAsmoutput;
  63. framereg : tregister;
  64. i,l : longint;
  65. procedure writeasmline;
  66. var
  67. i : longint;
  68. begin
  69. i:=length(s);
  70. while (i>0) and (s[i] in [' ',#9]) do
  71. dec(i);
  72. s[0]:=chr(i);
  73. if s<>'' then
  74. code.concat(Tai_direct.Create(strpnew(s)));
  75. { consider it set function set if the offset was loaded }
  76. if assigned(current_procinfo.procdef.funcretsym) and
  77. (pos(retstr,upper(s))>0) then
  78. tvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_assigned;
  79. s:='';
  80. end;
  81. begin
  82. ende:=false;
  83. framereg:=NR_FRAME_POINTER_REG;
  84. s:='';
  85. if assigned(current_procinfo.procdef.funcretsym) and
  86. is_fpu(current_procinfo.procdef.rettype.def) then
  87. tvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_assigned;
  88. { !!!!!
  89. if (not is_void(current_procinfo.procdef.rettype.def)) then
  90. retstr:=upper(tostr(tvarsym(current_procinfo.procdef.funcretsym).adjusted_address)+'('+gas_reg2str[procinfo^.framepointer]+')')
  91. else
  92. }
  93. retstr:='';
  94. c:=current_scanner.asmgetchar;
  95. code:=TAAsmoutput.Create;
  96. while not(ende) do
  97. begin
  98. { wrong placement
  99. current_scanner.gettokenpos; }
  100. case c of
  101. 'A'..'Z','a'..'z','_':
  102. begin
  103. current_scanner.gettokenpos;
  104. i:=0;
  105. hs:='';
  106. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  107. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  108. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  109. or (c='_') do
  110. begin
  111. inc(i);
  112. hs[i]:=c;
  113. c:=current_scanner.asmgetchar;
  114. end;
  115. hs[0]:=chr(i);
  116. if upper(hs)='END' then
  117. ende:=true
  118. else
  119. begin
  120. if c=':' then
  121. begin
  122. searchsym(upper(hs),srsym,srsymtable);
  123. if srsym<>nil then
  124. if (srsym.typ = labelsym) then
  125. Begin
  126. hs:=tlabelsym(srsym).lab.name;
  127. tlabelsym(srsym).lab.is_set:=true;
  128. end
  129. else
  130. Message(asmr_w_using_defined_as_local);
  131. end
  132. else
  133. { access to local variables }
  134. if assigned(current_procinfo.procdef) then
  135. begin
  136. { I don't know yet, what the ppc port requires }
  137. { we'll see how things settle down }
  138. { is the last written character an special }
  139. { char ? }
  140. { !!!
  141. if (s[length(s)]='%') and
  142. ret_in_acc(current_procinfo.procdef.rettype.def) and
  143. ((pos('AX',upper(hs))>0) or
  144. (pos('AL',upper(hs))>0)) then
  145. tfuncretsym(current_procinfo.procdef.funcretsym).funcretstate:=vs_assigned;
  146. }
  147. if ((s[length(s)]<>'0') or (hs[1]<>'x')) and not(is_register(hs)) then
  148. begin
  149. if assigned(current_procinfo.procdef.localst) and
  150. (current_procinfo.procdef.localst.symtablelevel >= normal_function_level) then
  151. sym:=tsym(current_procinfo.procdef.localst.search(upper(hs)))
  152. else
  153. sym:=nil;
  154. if assigned(sym) then
  155. begin
  156. if (sym.typ=labelsym) then
  157. Begin
  158. hs:=tlabelsym(sym).lab.name;
  159. end
  160. else if sym.typ=varsym then
  161. begin
  162. if (vo_is_external in tvarsym(sym).varoptions) then
  163. hs:=tvarsym(sym).mangledname
  164. else
  165. begin
  166. if (tvarsym(sym).reg<>NR_NO) then
  167. hs:=std_regname(framereg)
  168. else
  169. hs:=tostr(tvarsym(sym).address)+
  170. '('+std_regname(framereg)+')';
  171. end;
  172. end
  173. else
  174. { call to local function }
  175. if (sym.typ=procsym) and (pos('BL',upper(s))>0) then
  176. hs:=tprocsym(sym).first_procdef.mangledname;
  177. end
  178. else
  179. begin
  180. if assigned(current_procinfo.procdef.parast) then
  181. sym:=tsym(current_procinfo.procdef.parast.search(upper(hs)))
  182. else
  183. sym:=nil;
  184. if assigned(sym) then
  185. begin
  186. if sym.typ=varsym then
  187. begin
  188. l:=tvarsym(sym).address;
  189. { set offset }
  190. inc(l,current_procinfo.procdef.parast.address_fixup);
  191. // hs:=tostr(l)+'('+gas_reg2str[procinfo.framepointer.enum]+')';
  192. hs:=tostr(l)+'('+std_regname(framereg)+')';
  193. if pos(',',s) > 0 then
  194. tvarsym(sym).varstate:=vs_used;
  195. end;
  196. end
  197. { I added that but it creates a problem in line.ppi
  198. because there is a local label wbuffer and
  199. a static variable WBUFFER ...
  200. what would you decide, florian ?}
  201. else
  202. begin
  203. searchsym(upper(hs),sym,srsymtable);
  204. if assigned(sym) and (sym.owner.symtabletype in [globalsymtable,staticsymtable]) then
  205. begin
  206. case sym.typ of
  207. constsym :
  208. begin
  209. inc(tconstsym(sym).refs);
  210. case tconstsym(sym).consttyp of
  211. constint,constchar,constbool :
  212. hs:=tostr(tconstsym(sym).value.valueord);
  213. constpointer :
  214. hs:=tostr(tconstsym(sym).value.valueordptr);
  215. else
  216. Message(asmr_e_wrong_sym_type);
  217. end;
  218. end;
  219. varsym :
  220. begin
  221. Message2(asmr_h_direct_global_to_mangled,hs,tvarsym(sym).mangledname);
  222. hs:=tvarsym(sym).mangledname;
  223. inc(tvarsym(sym).refs);
  224. end;
  225. typedconstsym :
  226. begin
  227. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  228. hs:=ttypedconstsym(sym).mangledname;
  229. end;
  230. procsym :
  231. begin
  232. { procs can be called or the address can be loaded }
  233. if (pos('BL',upper(s))>0) {or (pos('LEA',upper(s))>0))} then
  234. begin
  235. if Tprocsym(sym).procdef_count>1 then
  236. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  237. Message2(asmr_h_direct_global_to_mangled,hs,tprocsym(sym).first_procdef.mangledname);
  238. hs:=tprocsym(sym).first_procdef.mangledname;
  239. end;
  240. end;
  241. else
  242. Message(asmr_e_wrong_sym_type);
  243. end;
  244. end
  245. {$ifdef dummy}
  246. else if upper(hs)='__SELF' then
  247. begin
  248. if assigned(procinfo^._class) then
  249. hs:=tostr(procinfo^.selfpointer_offset)+
  250. '('+gas_reg2str[procinfo^.framepointer]+')'
  251. else
  252. Message(asmr_e_cannot_use_SELF_outside_a_method);
  253. end
  254. else if upper(hs)='__RESULT' then
  255. begin
  256. if (not is_void(current_procinfo.procdef.rettype.def)) then
  257. hs:=retstr
  258. else
  259. Message(asmr_e_void_function);
  260. end
  261. { implement old stack/frame pointer access for nested procedures }
  262. {!!!!
  263. else if upper(hs)='__OLDSP' then
  264. begin
  265. { complicate to check there }
  266. { we do it: }
  267. if lexlevel>normal_function_level then
  268. hs:=tostr(procinfo^.framepointer_offset)+
  269. '('+gas_reg2str[procinfo^.framepointer]+')'
  270. else
  271. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  272. end;
  273. }
  274. end;
  275. {$endif dummy}
  276. end;
  277. end;
  278. end;
  279. end;
  280. s:=s+hs;
  281. end;
  282. end;
  283. ';',#10,#13:
  284. begin
  285. if pos(retstr,s) > 0 then
  286. tvarsym(current_procinfo.procdef.funcretsym).varstate:=vs_assigned;
  287. writeasmline;
  288. c:=current_scanner.asmgetchar;
  289. end;
  290. #26:
  291. Message(scan_f_end_of_file);
  292. else
  293. begin
  294. current_scanner.gettokenpos;
  295. inc(byte(s[0]));
  296. s[length(s)]:=c;
  297. c:=current_scanner.asmgetchar;
  298. end;
  299. end;
  300. end;
  301. writeasmline;
  302. assemble:=casmnode.create(code);
  303. end;
  304. {*****************************************************************************
  305. Initialize
  306. *****************************************************************************}
  307. const
  308. asmmode_arm_direct_info : tasmmodeinfo =
  309. (
  310. id : asmmode_direct;
  311. idtxt : 'DIRECT'
  312. );
  313. initialization
  314. RegisterAsmMode(asmmode_arm_direct_info);
  315. end.
  316. {
  317. $Log$
  318. Revision 1.4 2003-09-04 00:15:29 florian
  319. * first bunch of adaptions of arm compiler for new register type
  320. Revision 1.3 2003/09/01 15:11:17 florian
  321. * fixed reference handling
  322. * fixed operand postfix for floating point instructions
  323. * fixed wrong shifter constant handling
  324. Revision 1.2 2003/08/16 13:23:01 florian
  325. * several arm related stuff fixed
  326. Revision 1.1 2003/07/21 16:35:30 florian
  327. * very basic stuff for the arm
  328. }