radirect.pas 17 KB

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