radirect.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. {
  2. $Id$
  3. Copyright (c) 1998-2001 by Florian Klaempfl
  4. Reads inline 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. unit Radirect;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. node;
  23. function assemble : tnode;
  24. implementation
  25. uses
  26. { common }
  27. cutils,
  28. { global }
  29. globals,verbose,
  30. systems,
  31. { aasm }
  32. cpubase,aasmtai,
  33. { symtable }
  34. symconst,symbase,symtype,symsym,symtable,defbase,paramgr,
  35. { pass 1 }
  36. nbas,
  37. { parser }
  38. scanner,
  39. rax86_64,
  40. agx64att,
  41. { codegen }
  42. cgbase
  43. ;
  44. function assemble : tnode;
  45. var
  46. retstr,s,hs : string;
  47. c : char;
  48. ende : boolean;
  49. srsym,sym : tsym;
  50. srsymtable : tsymtable;
  51. code : TAAsmoutput;
  52. i,l : longint;
  53. procedure writeasmline;
  54. var
  55. i : longint;
  56. begin
  57. i:=length(s);
  58. while (i>0) and (s[i] in [' ',#9]) do
  59. dec(i);
  60. s[0]:=chr(i);
  61. if s<>'' then
  62. code.concat(Tai_direct.Create(strpnew(s)));
  63. { consider it set function set if the offset was loaded }
  64. if assigned(aktprocdef.funcretsym) and
  65. (pos(retstr,upper(s))>0) then
  66. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  67. s:='';
  68. end;
  69. begin
  70. ende:=false;
  71. s:='';
  72. if assigned(aktprocdef.funcretsym) and
  73. is_fpu(aktprocdef.rettype.def) then
  74. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  75. if (not is_void(aktprocdef.rettype.def)) then
  76. retstr:=upper(tostr(procinfo^.return_offset)+'('+att_reg2str[procinfo^.framepointer]+')')
  77. else
  78. retstr:='';
  79. c:=current_scanner.asmgetchar;
  80. code:=TAAsmoutput.Create;
  81. while not(ende) do
  82. begin
  83. { wrong placement
  84. current_scanner.gettokenpos; }
  85. case c of
  86. 'A'..'Z','a'..'z','_' : begin
  87. current_scanner.gettokenpos;
  88. i:=0;
  89. hs:='';
  90. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  91. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  92. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  93. or (c='_') do
  94. begin
  95. inc(i);
  96. hs[i]:=c;
  97. c:=current_scanner.asmgetchar;
  98. end;
  99. hs[0]:=chr(i);
  100. if upper(hs)='END' then
  101. ende:=true
  102. else
  103. begin
  104. if c=':' then
  105. begin
  106. searchsym(upper(hs),srsym,srsymtable);
  107. if srsym<>nil then
  108. if (srsym.typ = labelsym) then
  109. Begin
  110. hs:=tlabelsym(srsym).lab.name;
  111. tlabelsym(srsym).lab.is_set:=true;
  112. end
  113. else
  114. Message(asmr_w_using_defined_as_local);
  115. end
  116. else if upper(hs)='FWAIT' then
  117. FwaitWarning
  118. else
  119. { access to local variables }
  120. if assigned(aktprocdef) then
  121. begin
  122. { is the last written character an special }
  123. { char ? }
  124. if (s[length(s)]='%') and
  125. paramanager.ret_in_acc(aktprocdef.rettype.def) and
  126. ((pos('AX',upper(hs))>0) or
  127. (pos('AL',upper(hs))>0)) then
  128. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  129. if (s[length(s)]<>'%') and
  130. (s[length(s)]<>'$') and
  131. ((s[length(s)]<>'0') or (hs[1]<>'x')) then
  132. begin
  133. if assigned(aktprocdef.localst) and
  134. (lexlevel >= normal_function_level) then
  135. sym:=tsym(aktprocdef.localst.search(upper(hs)))
  136. else
  137. sym:=nil;
  138. if assigned(sym) then
  139. begin
  140. if (sym.typ = labelsym) then
  141. Begin
  142. hs:=tlabelsym(sym).lab.name;
  143. end
  144. else if sym.typ=varsym then
  145. begin
  146. {variables set are after a comma }
  147. {like in movl %eax,I }
  148. if pos(',',s) > 0 then
  149. tvarsym(sym).varstate:=vs_used
  150. else
  151. if (pos('MOV',upper(s)) > 0) and (tvarsym(sym).varstate=vs_declared) then
  152. Message1(sym_n_uninitialized_local_variable,hs);
  153. if (vo_is_external in tvarsym(sym).varoptions) then
  154. hs:=tvarsym(sym).mangledname
  155. else
  156. hs:='-'+tostr(tvarsym(sym).address)+
  157. '('+att_reg2str[procinfo^.framepointer]+')';
  158. end
  159. else
  160. { call to local function }
  161. if (sym.typ=procsym) and ((pos('CALL',upper(s))>0) or
  162. (pos('LEA',upper(s))>0)) then
  163. begin
  164. hs:=tprocsym(sym).defs^.def.mangledname;
  165. end;
  166. end
  167. else
  168. begin
  169. if assigned(aktprocdef.parast) then
  170. sym:=tsym(aktprocdef.parast.search(upper(hs)))
  171. else
  172. sym:=nil;
  173. if assigned(sym) then
  174. begin
  175. if sym.typ=varsym then
  176. begin
  177. l:=tvarsym(sym).address;
  178. { set offset }
  179. inc(l,aktprocdef.parast.address_fixup);
  180. hs:=tostr(l)+'('+att_reg2str[procinfo^.framepointer]+')';
  181. if pos(',',s) > 0 then
  182. tvarsym(sym).varstate:=vs_used;
  183. end;
  184. end
  185. { I added that but it creates a problem in line.ppi
  186. because there is a local label wbuffer and
  187. a static variable WBUFFER ...
  188. what would you decide, florian ?}
  189. else
  190. begin
  191. searchsym(upper(hs),sym,srsymtable);
  192. if assigned(sym) and (sym.owner.symtabletype in [globalsymtable,staticsymtable]) then
  193. begin
  194. case sym.typ of
  195. varsym :
  196. begin
  197. Message2(asmr_h_direct_global_to_mangled,hs,tvarsym(sym).mangledname);
  198. hs:=tvarsym(sym).mangledname;
  199. inc(tvarsym(sym).refs);
  200. end;
  201. typedconstsym :
  202. begin
  203. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  204. hs:=ttypedconstsym(sym).mangledname;
  205. end;
  206. procsym :
  207. begin
  208. { procs can be called or the address can be loaded }
  209. if ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  210. begin
  211. if assigned(tprocsym(sym).defs^.def) then
  212. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  213. Message2(asmr_h_direct_global_to_mangled,hs,tprocsym(sym).defs^.def.mangledname);
  214. hs:=tprocsym(sym).defs^.def.mangledname;
  215. end;
  216. end;
  217. else
  218. Message(asmr_e_wrong_sym_type);
  219. end;
  220. end
  221. else if upper(hs)='__SELF' then
  222. begin
  223. if assigned(procinfo^._class) then
  224. hs:=tostr(procinfo^.selfpointer_offset)+
  225. '('+att_reg2str[procinfo^.framepointer]+')'
  226. else
  227. Message(asmr_e_cannot_use_SELF_outside_a_method);
  228. end
  229. else if upper(hs)='__RESULT' then
  230. begin
  231. if (not is_void(aktprocdef.rettype.def)) then
  232. hs:=retstr
  233. else
  234. Message(asmr_e_void_function);
  235. end
  236. else if upper(hs)='__OLDEBP' then
  237. begin
  238. { complicate to check there }
  239. { we do it: }
  240. if lexlevel>normal_function_level then
  241. hs:=tostr(procinfo^.framepointer_offset)+
  242. '('+att_reg2str[procinfo^.framepointer]+')'
  243. else
  244. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  245. end;
  246. end;
  247. end;
  248. end;
  249. end;
  250. s:=s+hs;
  251. end;
  252. end;
  253. '{',';',#10,#13 : begin
  254. if pos(retstr,s) > 0 then
  255. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  256. writeasmline;
  257. c:=current_scanner.asmgetchar;
  258. end;
  259. #26 : Message(scan_f_end_of_file);
  260. else
  261. begin
  262. current_scanner.gettokenpos;
  263. inc(byte(s[0]));
  264. s[length(s)]:=c;
  265. c:=current_scanner.asmgetchar;
  266. end;
  267. end;
  268. end;
  269. writeasmline;
  270. assemble:=casmnode.create(code);
  271. end;
  272. {*****************************************************************************
  273. Initialize
  274. *****************************************************************************}
  275. const
  276. asmmode_i386_direct_info : tasmmodeinfo =
  277. (
  278. id : asmmode_direct;
  279. idtxt : 'DIRECT'
  280. );
  281. initialization
  282. RegisterAsmMode(asmmode_i386_direct_info);
  283. end.
  284. {
  285. $Log$
  286. Revision 1.1 2002-08-10 14:53:38 carl
  287. + moved target_cpu_string to cpuinfo
  288. * renamed asmmode enum.
  289. * assembler reader has now less ifdef's
  290. * move from nppcmem.pas -> ncgmem.pas vec. node.
  291. Revision 1.2 2002/07/25 22:55:34 florian
  292. * several fixes, small test units can be compiled
  293. Revision 1.1 2002/07/24 22:38:15 florian
  294. + initial release of x86-64 target code
  295. }