radirect.pas 20 KB

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