ra386dir.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. {
  2. $Id$
  3. Copyright (c) 1998 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 Ra386dir;
  19. interface
  20. uses
  21. tree;
  22. function assemble : ptree;
  23. implementation
  24. uses
  25. files,hcodegen,globals,scanner,aasm
  26. ,i386base,i386asm
  27. ,cobjects,symtable,types,verbose,
  28. rautils,ra386;
  29. function assemble : ptree;
  30. var
  31. retstr,s,hs : string;
  32. c : char;
  33. ende : boolean;
  34. sym : psym;
  35. code : paasmoutput;
  36. i,l : longint;
  37. procedure writeasmline;
  38. var
  39. i : longint;
  40. begin
  41. i:=length(s);
  42. while (i>0) and (s[i] in [' ',#9]) do
  43. dec(i);
  44. {$ifndef TP}
  45. {$ifopt H+}
  46. setlength(s,i);
  47. {$else}
  48. s[0]:=chr(i);
  49. {$endif}
  50. {$else}
  51. s[0]:=chr(i);
  52. {$endif}
  53. if s<>'' then
  54. code^.concat(new(pai_direct,init(strpnew(s))));
  55. { consider it set function set if the offset was loaded }
  56. if assigned(procinfo.retdef) and
  57. (pos(retstr,upper(s))>0) then
  58. procinfo.funcret_is_valid:=true;
  59. s:='';
  60. end;
  61. begin
  62. ende:=false;
  63. s:='';
  64. if assigned(procinfo.retdef) and
  65. is_fpu(procinfo.retdef) then
  66. procinfo.funcret_is_valid:=true;
  67. if assigned(procinfo.retdef) and
  68. (procinfo.retdef<>pdef(voiddef)) then
  69. retstr:=upper(tostr(procinfo.retoffset)+'('+att_reg2str[procinfo.framepointer]+')')
  70. else
  71. retstr:='';
  72. c:=current_scanner^.asmgetchar;
  73. code:=new(paasmoutput,init);
  74. while not(ende) do
  75. begin
  76. { wrong placement
  77. current_scanner^.gettokenpos; }
  78. case c of
  79. 'A'..'Z','a'..'z','_' : begin
  80. current_scanner^.gettokenpos;
  81. i:=0;
  82. hs:='';
  83. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  84. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  85. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  86. or (c='_') do
  87. begin
  88. inc(i);
  89. hs[i]:=c;
  90. c:=current_scanner^.asmgetchar;
  91. end;
  92. {$ifndef TP}
  93. {$ifopt H+}
  94. setlength(hs,i);
  95. {$else}
  96. hs[0]:=chr(i);
  97. {$endif}
  98. {$else}
  99. hs[0]:=chr(i);
  100. {$endif}
  101. if upper(hs)='END' then
  102. ende:=true
  103. else
  104. begin
  105. if c=':' then
  106. begin
  107. getsym(upper(hs),false);
  108. if srsym<>nil then
  109. if (srsym^.typ = labelsym) then
  110. Begin
  111. hs:=plabelsym(srsym)^.lab^.name;
  112. plabelsym(srsym)^.lab^.is_set:=true;
  113. end
  114. else
  115. Message(asmr_w_using_defined_as_local);
  116. end
  117. else if upper(hs)='FWAIT' then
  118. FwaitWarning
  119. else
  120. { access to local variables }
  121. if assigned(aktprocsym) then
  122. begin
  123. { is the last written character an special }
  124. { char ? }
  125. if (s[length(s)]='%') and
  126. ret_in_acc(procinfo.retdef) and
  127. ((pos('AX',upper(hs))>0) or
  128. (pos('AL',upper(hs))>0)) then
  129. procinfo.funcret_is_valid:=true;
  130. if (s[length(s)]<>'%') and
  131. (s[length(s)]<>'$') and
  132. ((s[length(s)]<>'0') or (hs[1]<>'x')) then
  133. begin
  134. if assigned(aktprocsym^.definition^.localst) and
  135. (lexlevel >= normal_function_level) then
  136. sym:=aktprocsym^.definition^.localst^.search(upper(hs))
  137. else
  138. sym:=nil;
  139. if assigned(sym) then
  140. begin
  141. if (sym^.typ = labelsym) then
  142. Begin
  143. hs:=plabelsym(sym)^.lab^.name;
  144. end
  145. else if sym^.typ=varsym then
  146. begin
  147. {variables set are after a comma }
  148. {like in movl %eax,I }
  149. if pos(',',s) > 0 then
  150. pvarsym(sym)^.is_valid:=1
  151. else
  152. if (pos('MOV',upper(s)) > 0) and (pvarsym(sym)^.is_valid=0) then
  153. Message1(sym_n_uninitialized_local_variable,hs);
  154. if ((pvarsym(sym)^.var_options and vo_is_external)<>0) then
  155. hs:=pvarsym(sym)^.mangledname
  156. else
  157. hs:='-'+tostr(pvarsym(sym)^.address)+'('+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:=pprocsym(sym)^.definition^.mangledname;
  165. end;
  166. end
  167. else
  168. begin
  169. if assigned(aktprocsym^.definition^.parast) then
  170. sym:=aktprocsym^.definition^.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:=pvarsym(sym)^.address;
  178. { set offset }
  179. inc(l,aktprocsym^.definition^.parast^.address_fixup);
  180. hs:=tostr(l)+'('+att_reg2str[procinfo.framepointer]+')';
  181. if pos(',',s) > 0 then
  182. pvarsym(sym)^.is_valid:=1;
  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. {$ifndef IGNOREGLOBALVAR}
  192. getsym(upper(hs),false);
  193. sym:=srsym;
  194. if assigned(sym) and (sym^.owner^.symtabletype in [unitsymtable,
  195. globalsymtable,staticsymtable]) then
  196. begin
  197. if (sym^.typ = varsym) or (sym^.typ = typedconstsym) then
  198. begin
  199. Message2(asmr_h_direct_global_to_mangled,hs,sym^.mangledname);
  200. hs:=sym^.mangledname;
  201. if sym^.typ=varsym then
  202. inc(pvarsym(sym)^.refs);
  203. end;
  204. { procs can be called or the address can be loaded }
  205. if (sym^.typ=procsym) and
  206. ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  207. begin
  208. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  209. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  210. Message2(asmr_h_direct_global_to_mangled,hs,sym^.mangledname);
  211. hs:=sym^.mangledname;
  212. end;
  213. end
  214. else
  215. {$endif TESTGLOBALVAR}
  216. if upper(hs)='__SELF' then
  217. begin
  218. if assigned(procinfo._class) then
  219. hs:=tostr(procinfo.ESI_offset)+'('+att_reg2str[procinfo.framepointer]+')'
  220. else
  221. Message(asmr_e_cannot_use_SELF_outside_a_method);
  222. end
  223. else if upper(hs)='__RESULT' then
  224. begin
  225. if assigned(procinfo.retdef) and
  226. (procinfo.retdef<>pdef(voiddef)) then
  227. hs:=retstr
  228. else
  229. Message(asmr_e_void_function);
  230. end
  231. else if upper(hs)='__OLDEBP' then
  232. begin
  233. { complicate to check there }
  234. { we do it: }
  235. if lexlevel>normal_function_level then
  236. hs:=tostr(procinfo.framepointer_offset)+
  237. '('+att_reg2str[procinfo.framepointer]+')'
  238. else
  239. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  240. end;
  241. end;
  242. end;
  243. end;
  244. end;
  245. s:=s+hs;
  246. end;
  247. end;
  248. '{',';',#10,#13 : begin
  249. if pos(retstr,s) > 0 then
  250. procinfo.funcret_is_valid:=true;
  251. writeasmline;
  252. c:=current_scanner^.asmgetchar;
  253. end;
  254. #26 : Message(scan_f_end_of_file);
  255. else
  256. begin
  257. current_scanner^.gettokenpos;
  258. {$ifndef TP}
  259. {$ifopt H+}
  260. setlength(s,length(s)+1);
  261. {$else}
  262. inc(byte(s[0]));
  263. {$endif}
  264. {$else}
  265. inc(byte(s[0]));
  266. {$endif}
  267. s[length(s)]:=c;
  268. c:=current_scanner^.asmgetchar;
  269. end;
  270. end;
  271. end;
  272. writeasmline;
  273. assemble:=genasmnode(code);
  274. end;
  275. end.
  276. {
  277. $Log$
  278. Revision 1.21 1999-05-27 19:44:57 peter
  279. * removed oldasm
  280. * plabel -> pasmlabel
  281. * -a switches to source writing automaticly
  282. * assembler readers OOPed
  283. * asmsymbol automaticly external
  284. * jumptables and other label fixes for asm readers
  285. Revision 1.20 1999/05/21 13:55:15 peter
  286. * NEWLAB for label as symbol
  287. Revision 1.19 1999/05/05 22:22:02 peter
  288. * updated messages
  289. Revision 1.18 1999/05/01 13:24:40 peter
  290. * merged nasm compiler
  291. * old asm moved to oldasm/
  292. Revision 1.17 1999/03/31 13:55:18 peter
  293. * assembler inlining working for ag386bin
  294. Revision 1.16 1999/03/24 23:17:22 peter
  295. * fixed bugs 212,222,225,227,229,231,233
  296. Revision 1.15 1999/03/01 13:22:26 pierre
  297. * varsym refs incremented
  298. Revision 1.14 1999/02/22 02:15:36 peter
  299. * updates for ag386bin
  300. Revision 1.13 1999/01/27 13:04:12 pierre
  301. * bug with static vars in assembler readers
  302. Revision 1.12 1999/01/10 15:37:57 peter
  303. * moved some tables from ra386*.pas -> i386.pas
  304. + start of coff writer
  305. * renamed asmutils unit to rautils
  306. Revision 1.11 1998/11/17 00:26:12 peter
  307. * fixed for $H+
  308. Revision 1.10 1998/11/13 15:40:28 pierre
  309. + added -Se in Makefile cvstest target
  310. + lexlevel cleanup
  311. normal_function_level main_program_level and unit_init_level defined
  312. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  313. (test added in code !)
  314. * -Un option was wrong
  315. * _FAIL and _SELF only keyword inside
  316. constructors and methods respectively
  317. Revision 1.9 1998/10/20 08:06:57 pierre
  318. * several memory corruptions due to double freemem solved
  319. => never use p^.loc.location:=p^.left^.loc.location;
  320. + finally I added now by default
  321. that ra386dir translates global and unit symbols
  322. + added a first field in tsymtable and
  323. a nextsym field in tsym
  324. (this allows to obtain ordered type info for
  325. records and objects in gdb !)
  326. Revision 1.8 1998/09/04 08:42:08 peter
  327. * updated some error messages
  328. Revision 1.7 1998/09/03 17:39:05 florian
  329. + better code for type conversation longint/dword to real type
  330. Revision 1.6 1998/09/03 17:08:47 pierre
  331. * better lines for stabs
  332. (no scroll back to if before else part
  333. no return to case line at jump outside case)
  334. + source lines also if not in order
  335. Revision 1.5 1998/08/21 08:45:51 pierre
  336. * better line info for asm statements
  337. Revision 1.4 1998/07/14 14:46:59 peter
  338. * released NEWINPUT
  339. Revision 1.3 1998/07/07 11:20:08 peter
  340. + NEWINPUT for a better inputfile and scanner object
  341. Revision 1.2 1998/06/24 14:06:37 peter
  342. * fixed the name changes
  343. Revision 1.1 1998/06/23 14:00:18 peter
  344. * renamed RA* units
  345. Revision 1.5 1998/06/12 10:32:32 pierre
  346. * column problem hopefully solved
  347. + C vars declaration changed
  348. Revision 1.4 1998/06/04 23:51:58 peter
  349. * m68k compiles
  350. + .def file creation moved to gendef.pas so it could also be used
  351. for win32
  352. }