radirect.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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. aasmbase,aasmtai,aasmcpu,
  33. { symtable }
  34. symconst,symbase,symtype,symsym,symtable,defutil,paramgr,
  35. { pass 1 }
  36. nbas,
  37. { parser }
  38. scanner,
  39. ra386,
  40. { codegen }
  41. cgbase,
  42. { constants }
  43. ag386att,
  44. cpubase
  45. ;
  46. function assemble : tnode;
  47. var
  48. retstr,s,hs : string;
  49. c : char;
  50. ende : boolean;
  51. srsym,sym : tsym;
  52. srsymtable : tsymtable;
  53. code : TAAsmoutput;
  54. framereg : tregister;
  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. framereg:=procinfo.framepointer;
  79. convert_register_to_enum(framereg);
  80. if (not is_void(aktprocdef.rettype.def)) then
  81. retstr:=upper(tostr(procinfo.return_offset)+'('+gas_reg2str[framereg.enum]+')')
  82. else
  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','_' : begin
  92. current_scanner.gettokenpos;
  93. i:=0;
  94. hs:='';
  95. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  96. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  97. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  98. or (c='_') do
  99. begin
  100. inc(i);
  101. hs[i]:=c;
  102. c:=current_scanner.asmgetchar;
  103. end;
  104. hs[0]:=chr(i);
  105. if upper(hs)='END' then
  106. ende:=true
  107. else
  108. begin
  109. if c=':' then
  110. begin
  111. searchsym(upper(hs),srsym,srsymtable);
  112. if srsym<>nil then
  113. if (srsym.typ = labelsym) then
  114. Begin
  115. hs:=tlabelsym(srsym).lab.name;
  116. tlabelsym(srsym).lab.is_set:=true;
  117. end
  118. else
  119. Message(asmr_w_using_defined_as_local);
  120. end
  121. else if upper(hs)='FWAIT' then
  122. FwaitWarning
  123. else
  124. { access to local variables }
  125. if assigned(aktprocdef) then
  126. begin
  127. { is the last written character an special }
  128. { char ? }
  129. if (s[length(s)]='%') and
  130. paramanager.ret_in_acc(aktprocdef.rettype.def,aktprocdef.proccalloption) and
  131. ((pos('AX',upper(hs))>0) or
  132. (pos('AL',upper(hs))>0)) then
  133. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  134. if (s[length(s)]<>'%') and
  135. (s[length(s)]<>'$') and
  136. ((s[length(s)]<>'0') or (hs[1]<>'x')) then
  137. begin
  138. if assigned(aktprocdef.localst) and
  139. (lexlevel >= normal_function_level) then
  140. sym:=tsym(aktprocdef.localst.search(upper(hs)))
  141. else
  142. sym:=nil;
  143. if assigned(sym) then
  144. begin
  145. if (sym.typ = labelsym) then
  146. Begin
  147. hs:=tlabelsym(sym).lab.name;
  148. end
  149. else if sym.typ=varsym then
  150. begin
  151. {variables set are after a comma }
  152. {like in movl %eax,I }
  153. if pos(',',s) > 0 then
  154. tvarsym(sym).varstate:=vs_used
  155. else
  156. if (pos('MOV',upper(s)) > 0) and (tvarsym(sym).varstate=vs_declared) then
  157. Message1(sym_n_uninitialized_local_variable,hs);
  158. if (vo_is_external in tvarsym(sym).varoptions) then
  159. hs:=tvarsym(sym).mangledname
  160. else
  161. hs:='-'+tostr(tvarsym(sym).address)+
  162. '('+gas_reg2str[framereg.enum]+')';
  163. end
  164. else
  165. { call to local function }
  166. if (sym.typ=procsym) and ((pos('CALL',upper(s))>0) or
  167. (pos('LEA',upper(s))>0)) then
  168. begin
  169. hs:=tprocsym(sym).first_procdef.mangledname;
  170. end;
  171. end
  172. else
  173. begin
  174. if assigned(aktprocdef.parast) then
  175. sym:=tsym(aktprocdef.parast.search(upper(hs)))
  176. else
  177. sym:=nil;
  178. if assigned(sym) then
  179. begin
  180. if sym.typ=varsym then
  181. begin
  182. l:=tvarsym(sym).address;
  183. { set offset }
  184. inc(l,aktprocdef.parast.address_fixup);
  185. hs:=tostr(l)+'('+gas_reg2str[framereg.enum]+')';
  186. if pos(',',s) > 0 then
  187. tvarsym(sym).varstate:=vs_used;
  188. end;
  189. end
  190. { I added that but it creates a problem in line.ppi
  191. because there is a local label wbuffer and
  192. a static variable WBUFFER ...
  193. what would you decide, florian ?}
  194. else
  195. begin
  196. searchsym(upper(hs),sym,srsymtable);
  197. if assigned(sym) and (sym.owner.symtabletype in [globalsymtable,staticsymtable]) then
  198. begin
  199. case sym.typ of
  200. varsym :
  201. begin
  202. Message2(asmr_h_direct_global_to_mangled,hs,tvarsym(sym).mangledname);
  203. hs:=tvarsym(sym).mangledname;
  204. inc(tvarsym(sym).refs);
  205. end;
  206. typedconstsym :
  207. begin
  208. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  209. hs:=ttypedconstsym(sym).mangledname;
  210. end;
  211. procsym :
  212. begin
  213. { procs can be called or the address can be loaded }
  214. if ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  215. begin
  216. if tprocsym(sym).procdef_count>1 then
  217. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  218. Message2(asmr_h_direct_global_to_mangled,hs,tprocsym(sym).first_procdef.mangledname);
  219. hs:=tprocsym(sym).first_procdef.mangledname;
  220. end;
  221. end;
  222. else
  223. Message(asmr_e_wrong_sym_type);
  224. end;
  225. end
  226. else if upper(hs)='__SELF' then
  227. begin
  228. if assigned(procinfo._class) then
  229. hs:=tostr(procinfo.selfpointer_offset)+
  230. '('+gas_reg2str[framereg.enum]+')'
  231. else
  232. Message(asmr_e_cannot_use_SELF_outside_a_method);
  233. end
  234. else if upper(hs)='__RESULT' then
  235. begin
  236. if (not is_void(aktprocdef.rettype.def)) then
  237. hs:=retstr
  238. else
  239. Message(asmr_e_void_function);
  240. end
  241. else if upper(hs)='__OLDEBP' 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[framereg.enum]+')'
  248. else
  249. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  250. end;
  251. end;
  252. end;
  253. end;
  254. end;
  255. s:=s+hs;
  256. end;
  257. end;
  258. '{',';',#10,#13 : begin
  259. if pos(retstr,s) > 0 then
  260. tfuncretsym(aktprocdef.funcretsym).funcretstate:=vs_assigned;
  261. writeasmline;
  262. c:=current_scanner.asmgetchar;
  263. end;
  264. #26 : Message(scan_f_end_of_file);
  265. else
  266. begin
  267. current_scanner.gettokenpos;
  268. inc(byte(s[0]));
  269. s[length(s)]:=c;
  270. c:=current_scanner.asmgetchar;
  271. end;
  272. end;
  273. end;
  274. writeasmline;
  275. assemble:=casmnode.create(code);
  276. end;
  277. {*****************************************************************************
  278. Initialize
  279. *****************************************************************************}
  280. const
  281. asmmode_i386_direct_info : tasmmodeinfo =
  282. (
  283. id : asmmode_direct;
  284. idtxt : 'DIRECT'
  285. );
  286. initialization
  287. RegisterAsmMode(asmmode_i386_direct_info);
  288. end.
  289. {
  290. $Log$
  291. Revision 1.7 2003-04-21 20:05:10 peter
  292. * removed some ie checks
  293. Revision 1.6 2003/01/08 18:43:57 daniel
  294. * Tregister changed into a record
  295. Revision 1.5 2002/11/25 17:43:27 peter
  296. * splitted defbase in defutil,symutil,defcmp
  297. * merged isconvertable and is_equal into compare_defs(_ext)
  298. * made operator search faster by walking the list only once
  299. Revision 1.4 2002/11/18 17:32:00 peter
  300. * pass proccalloption to ret_in_xxx and push_xxx functions
  301. Revision 1.3 2002/09/03 16:26:28 daniel
  302. * Make Tprocdef.defs protected
  303. Revision 1.2 2002/08/17 09:23:47 florian
  304. * first part of procinfo rewrite
  305. Revision 1.1 2002/08/10 14:47:50 carl
  306. + moved target_cpu_string to cpuinfo
  307. * renamed asmmode enum.
  308. * assembler reader has now less ifdef's
  309. * move from nppcmem.pas -> ncgmem.pas vec. node.
  310. Revision 1.21 2002/07/20 11:58:05 florian
  311. * types.pas renamed to defbase.pas because D6 contains a types
  312. unit so this would conflicts if D6 programms are compiled
  313. + Willamette/SSE2 instructions to assembler added
  314. Revision 1.20 2002/07/11 14:41:34 florian
  315. * start of the new generic parameter handling
  316. Revision 1.19 2002/07/01 18:46:34 peter
  317. * internal linker
  318. * reorganized aasm layer
  319. Revision 1.18 2002/05/18 13:34:26 peter
  320. * readded missing revisions
  321. Revision 1.17 2002/05/16 19:46:52 carl
  322. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  323. + try to fix temp allocation (still in ifdef)
  324. + generic constructor calls
  325. + start of tassembler / tmodulebase class cleanup
  326. Revision 1.15 2002/05/12 16:53:18 peter
  327. * moved entry and exitcode to ncgutil and cgobj
  328. * foreach gets extra argument for passing local data to the
  329. iterator function
  330. * -CR checks also class typecasts at runtime by changing them
  331. into as
  332. * fixed compiler to cycle with the -CR option
  333. * fixed stabs with elf writer, finally the global variables can
  334. be watched
  335. * removed a lot of routines from cga unit and replaced them by
  336. calls to cgobj
  337. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  338. u32bit then the other is typecasted also to u32bit without giving
  339. a rangecheck warning/error.
  340. * fixed pascal calling method with reversing also the high tree in
  341. the parast, detected by tcalcst3 test
  342. Revision 1.14 2002/04/15 19:12:09 carl
  343. + target_info.size_of_pointer -> pointer_size
  344. + some cleanup of unused types/variables
  345. * move several constants from cpubase to their specific units
  346. (where they are used)
  347. + att_Reg2str -> gas_reg2str
  348. + int_reg2str -> std_reg2str
  349. Revision 1.13 2002/04/14 17:01:52 carl
  350. + att_reg2str -> gas_reg2str
  351. }