radirect.pas 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. {*****************************************************************************}
  2. { File : radirect.pas }
  3. { Author : Mazen NEIFER }
  4. { Project : Free Pascal Compiler (FPC) }
  5. { Creation date : 2002\08\22 }
  6. { Last modification date : 2002\08\22 }
  7. { Licence : GPL }
  8. { Bug report : [email protected] }
  9. {*****************************************************************************}
  10. {
  11. $Id$
  12. Copyright (c) 1998-2002 by Florian Klaempfl
  13. Reads inline assembler and writes the lines direct to the output
  14. This program is free software; you can redistribute it and/or modify
  15. it under the terms of the GNU General Public License as published by
  16. the Free Software Foundation; either version 2 of the License, or
  17. (at your option) any later version.
  18. This program is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program; if not, write to the Free Software
  24. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. ****************************************************************************}
  26. unit radirect;
  27. {$MACRO ON}{$i fpcdefs.inc}
  28. interface
  29. uses
  30. node;
  31. function assemble : tnode;
  32. implementation
  33. uses
  34. { common }
  35. cutils,
  36. { global }
  37. globals,verbose,
  38. systems,
  39. { aasm }
  40. aasmbase,aasmtai,aasmcpu,
  41. { symtable }
  42. symconst,symbase,symtype,symsym,symtable,defutil,paramgr,
  43. { pass 1 }
  44. nbas,
  45. { parser }
  46. scanner,
  47. rautils,
  48. { codegen }
  49. cgbase,
  50. { constants }
  51. aggas,cpubase,globtype
  52. ;
  53. function assemble : tnode;
  54. var
  55. uhs,
  56. retstr,s,hs : string;
  57. c : char;
  58. ende : boolean;
  59. srsym,sym : tsym;
  60. srsymtable : tsymtable;
  61. code : TAAsmoutput;
  62. i,l : longint;
  63. procedure writeasmline;
  64. var
  65. i : longint;
  66. begin
  67. i:=length(s);
  68. while (i>0) and (s[i] in [' ',#9]) do
  69. dec(i);
  70. s[0]:=chr(i);
  71. if s<>'' then
  72. code.concat(Tai_direct.Create(strpnew(s)));
  73. { consider it set function set if the offset was loaded }
  74. if assigned(current_procdef.funcretsym) and
  75. (pos(retstr,upper(s))>0) then
  76. tvarsym(current_procdef.funcretsym).varstate:=vs_assigned;
  77. s:='';
  78. end;
  79. begin
  80. ende:=false;
  81. s:='';
  82. if assigned(current_procdef.funcretsym) and
  83. is_fpu(current_procdef.rettype.def) then
  84. tvarsym(current_procdef.funcretsym).varstate:=vs_assigned;
  85. if (not is_void(current_procdef.rettype.def)) then
  86. retstr:=upper(tostr(current_procinfo.return_offset)+'('+std_reg2str[current_procinfo.framepointer.enum]+')')
  87. else
  88. retstr:='';
  89. c:=current_scanner.asmgetchar;
  90. code:=TAAsmoutput.Create;
  91. while not(ende) do
  92. begin
  93. { wrong placement
  94. current_scanner.gettokenpos; }
  95. case c of
  96. 'A'..'Z','a'..'z','_' : begin
  97. current_scanner.gettokenpos;
  98. i:=0;
  99. hs:='';
  100. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  101. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  102. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  103. or (c='_') do
  104. begin
  105. inc(i);
  106. hs[i]:=c;
  107. c:=current_scanner.asmgetchar;
  108. end;
  109. hs[0]:=chr(i);
  110. if upper(hs)='END' then
  111. ende:=true
  112. else
  113. begin
  114. if c=':' then
  115. begin
  116. searchsym(upper(hs),srsym,srsymtable);
  117. if srsym<>nil then
  118. if (srsym.typ = labelsym) then
  119. Begin
  120. hs:=tlabelsym(srsym).lab.name;
  121. tlabelsym(srsym).lab.is_set:=true;
  122. end
  123. else
  124. Message(asmr_w_using_defined_as_local);
  125. end
  126. else
  127. { access to local variables }
  128. if assigned(current_procdef) then
  129. begin
  130. { is the last written character an special }
  131. { char ? }
  132. if (s[length(s)]='%') and
  133. (not paramanager.ret_in_param(current_procdef.rettype.def,current_procdef.proccalloption)) and
  134. ((pos('AX',upper(hs))>0) or
  135. (pos('AL',upper(hs))>0)) then
  136. tvarsym(current_procdef.funcretsym).varstate:=vs_assigned;
  137. if (s[length(s)]<>'%') and
  138. (s[length(s)]<>'$') and
  139. ((s[length(s)]<>'0') or (hs[1]<>'x')) then
  140. begin
  141. if assigned(current_procdef.localst) and
  142. (current_procdef.localst.symtablelevel >= normal_function_level) then
  143. sym:=tsym(current_procdef.localst.search(upper(hs)))
  144. else
  145. sym:=nil;
  146. if assigned(sym) then
  147. begin
  148. if (sym.typ = labelsym) then
  149. Begin
  150. hs:=tlabelsym(sym).lab.name;
  151. end
  152. else if sym.typ=varsym then
  153. begin
  154. {variables set are after a comma }
  155. {like in movl %eax,I }
  156. if pos(',',s) > 0 then
  157. tvarsym(sym).varstate:=vs_used
  158. else
  159. if (pos('MOV',upper(s)) > 0) and (tvarsym(sym).varstate=vs_declared) then
  160. Message1(sym_n_uninitialized_local_variable,hs);
  161. if (vo_is_external in tvarsym(sym).varoptions) then
  162. hs:=tvarsym(sym).mangledname
  163. else
  164. hs:='-'+tostr(tvarsym(sym).address)+
  165. '('+std_reg2str[current_procinfo.framepointer.enum]+')';
  166. end
  167. else
  168. { call to local function }
  169. if (sym.typ=procsym) and ((pos('CALL',upper(s))>0) or
  170. (pos('LEA',upper(s))>0)) then
  171. begin
  172. hs:=tprocsym(sym).first_procdef.mangledname;
  173. end;
  174. end
  175. else
  176. begin
  177. if assigned(current_procdef.parast) then
  178. sym:=tsym(current_procdef.parast.search(upper(hs)))
  179. else
  180. sym:=nil;
  181. if assigned(sym) then
  182. begin
  183. if sym.typ=varsym then
  184. begin
  185. l:=tvarsym(sym).address;
  186. { set offset }
  187. inc(l,current_procdef.parast.address_fixup);
  188. hs:=tostr(l)+'('+std_reg2str[current_procinfo.framepointer.enum]+')';
  189. if pos(',',s) > 0 then
  190. tvarsym(sym).varstate:=vs_used;
  191. end;
  192. end
  193. end
  194. end
  195. else
  196. begin
  197. uhs:=upper(hs);
  198. if (uhs='__SELF') then
  199. begin
  200. if assigned(current_procdef._class) then
  201. uhs:='self'
  202. else
  203. begin
  204. Message(asmr_e_cannot_use_SELF_outside_a_method);
  205. uhs:='';
  206. end;
  207. end
  208. else
  209. if (uhs='__OLDEBP') then
  210. begin
  211. if current_procdef.parast.symtablelevel>normal_function_level then
  212. uhs:='parentframe'
  213. else
  214. begin
  215. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  216. uhs:='';
  217. end;
  218. end
  219. else
  220. if uhs='__RESULT' then
  221. begin
  222. if (not is_void(current_procdef.rettype.def)) then
  223. uhs:='result'
  224. else
  225. begin
  226. Message(asmr_e_void_function);
  227. uhs:='';
  228. end;
  229. end;
  230. if uhs<>'' then
  231. searchsym(upper(hs),sym,srsymtable)
  232. else
  233. sym:=nil;
  234. if assigned(sym) then
  235. begin
  236. case sym.owner.symtabletype of
  237. globalsymtable,
  238. staticsymtable :
  239. begin
  240. case sym.typ of
  241. varsym :
  242. begin
  243. Message2(asmr_h_direct_global_to_mangled,hs,tvarsym(sym).mangledname);
  244. hs:=tvarsym(sym).mangledname;
  245. inc(tvarsym(sym).refs);
  246. end;
  247. typedconstsym :
  248. begin
  249. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  250. hs:=ttypedconstsym(sym).mangledname;
  251. end;
  252. procsym :
  253. begin
  254. { procs can be called or the address can be loaded }
  255. if ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  256. begin
  257. if tprocsym(sym).procdef_count>1 then
  258. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  259. Message2(asmr_h_direct_global_to_mangled,hs,tprocsym(sym).first_procdef.mangledname);
  260. hs:=tprocsym(sym).first_procdef.mangledname;
  261. end;
  262. end;
  263. else
  264. Message(asmr_e_wrong_sym_type);
  265. end;
  266. end;
  267. parasymtable,
  268. localsymtable :
  269. begin
  270. case sym.typ of
  271. varsym :
  272. begin
  273. hs:=tostr(tvarsym(sym).adjusted_address)+
  274. '('+std_reg2str[framereg.enum]+')';
  275. inc(tvarsym(sym).refs);
  276. end;
  277. typedconstsym :
  278. begin
  279. Message2(asmr_h_direct_global_to_mangled,hs,ttypedconstsym(sym).mangledname);
  280. hs:=ttypedconstsym(sym).mangledname;
  281. end;
  282. else
  283. Message(asmr_e_wrong_sym_type);
  284. end;
  285. end;
  286. end;
  287. end;
  288. end;
  289. end;
  290. end;
  291. s:=s+hs;
  292. end;
  293. end;
  294. '{',';',#10,#13 : begin
  295. if pos(retstr,s) > 0 then
  296. tvarsym(current_procdef.funcretsym).varstate:=vs_assigned;
  297. writeasmline;
  298. c:=current_scanner.asmgetchar;
  299. end;
  300. #26 : Message(scan_f_end_of_file);
  301. else
  302. begin
  303. current_scanner.gettokenpos;
  304. inc(byte(s[0]));
  305. s[length(s)]:=c;
  306. c:=current_scanner.asmgetchar;
  307. end;
  308. end;
  309. end;
  310. writeasmline;
  311. assemble:=casmnode.create(code);
  312. end;
  313. {*****************************************************************************
  314. Initialize
  315. *****************************************************************************}
  316. const
  317. asmmode_i386_direct_info : tasmmodeinfo =
  318. (
  319. id : asmmode_direct;
  320. idtxt : 'DIRECT'
  321. );
  322. initialization
  323. RegisterAsmMode(asmmode_i386_direct_info);
  324. end.
  325. {
  326. $Log$
  327. Revision 1.8 2003-05-22 16:11:22 florian
  328. * fixed sparc compilation partially
  329. Revision 1.7 2003/04/27 11:21:36 peter
  330. * aktprocdef renamed to current_procdef
  331. * procinfo renamed to current_procinfo
  332. * procinfo will now be stored in current_module so it can be
  333. cleaned up properly
  334. * gen_main_procsym changed to create_main_proc and release_main_proc
  335. to also generate a tprocinfo structure
  336. * fixed unit implicit initfinal
  337. Revision 1.6 2003/04/27 07:48:05 peter
  338. * updated for removed lexlevel
  339. Revision 1.5 2003/01/08 18:43:58 daniel
  340. * Tregister changed into a record
  341. Revision 1.4 2002/11/25 17:43:29 peter
  342. * splitted defbase in defutil,symutil,defcmp
  343. * merged isconvertable and is_equal into compare_defs(_ext)
  344. * made operator search faster by walking the list only once
  345. Revision 1.3 2002/11/18 17:32:01 peter
  346. * pass proccalloption to ret_in_xxx and push_xxx functions
  347. Revision 1.2 2002/09/19 20:24:47 mazen
  348. + call support
  349. Revision 1.1 2002/08/23 10:08:28 mazen
  350. *** empty log message ***
  351. Revision 1.2 2002/08/17 09:23:47 florian
  352. * first part of procinfo rewrite
  353. Revision 1.1 2002/08/10 14:47:50 carl
  354. + moved target_cpu_string to cpuinfo
  355. * renamed asmmode enum.
  356. * assembler reader has now less ifdef's
  357. * move from nppcmem.pas -> ncgmem.pas vec. node.
  358. Revision 1.21 2002/07/20 11:58:05 florian
  359. * types.pas renamed to defbase.pas because D6 contains a types
  360. unit so this would conflicts if D6 programms are compiled
  361. + Willamette/SSE2 instructions to assembler added
  362. Revision 1.20 2002/07/11 14:41:34 florian
  363. * start of the new generic parameter handling
  364. Revision 1.19 2002/07/01 18:46:34 peter
  365. * internal linker
  366. * reorganized aasm layer
  367. Revision 1.18 2002/05/18 13:34:26 peter
  368. * readded missing revisions
  369. Revision 1.17 2002/05/16 19:46:52 carl
  370. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  371. + try to fix temp allocation (still in ifdef)
  372. + generic constructor calls
  373. + start of tassembler / tmodulebase class cleanup
  374. Revision 1.15 2002/05/12 16:53:18 peter
  375. * moved entry and exitcode to ncgutil and cgobj
  376. * foreach gets extra argument for passing local data to the
  377. iterator function
  378. * -CR checks also class typecasts at runtime by changing them
  379. into as
  380. * fixed compiler to cycle with the -CR option
  381. * fixed stabs with elf writer, finally the global variables can
  382. be watched
  383. * removed a lot of routines from cga unit and replaced them by
  384. calls to cgobj
  385. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  386. u32bit then the other is typecasted also to u32bit without giving
  387. a rangecheck warning/error.
  388. * fixed pascal calling method with reversing also the high tree in
  389. the parast, detected by tcalcst3 test
  390. Revision 1.14 2002/04/15 19:12:09 carl
  391. + target_info.size_of_pointer -> pointer_size
  392. + some cleanup of unused types/variables
  393. * move several constants from cpubase to their specific units
  394. (where they are used)
  395. + att_Reg2str -> std_reg2str
  396. + int_reg2str -> std_reg2str
  397. Revision 1.13 2002/04/14 17:01:52 carl
  398. + att_reg2str -> std_reg2str
  399. }