ra386dir.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. comphook,files,hcodegen,globals,scanner,aasm
  26. {$ifdef Ag386Bin}
  27. ,i386base,i386asm
  28. {$else}
  29. ,i386
  30. {$endif}
  31. ,cobjects,symtable,types,verbose,rautils;
  32. function assemble : ptree;
  33. var
  34. retstr,s,hs : string;
  35. c : char;
  36. ende : boolean;
  37. sym : psym;
  38. code : paasmoutput;
  39. i,l : longint;
  40. procedure writeasmline;
  41. var
  42. i : longint;
  43. begin
  44. i:=length(s);
  45. while (i>0) and (s[i] in [' ',#9]) do
  46. dec(i);
  47. {$ifndef TP}
  48. {$ifopt H+}
  49. setlength(s,i);
  50. {$else}
  51. s[0]:=chr(i);
  52. {$endif}
  53. {$else}
  54. s[0]:=chr(i);
  55. {$endif}
  56. if s<>'' then
  57. code^.concat(new(pai_direct,init(strpnew(s))));
  58. { consider it set function set if the offset was loaded }
  59. if assigned(procinfo.retdef) and
  60. (pos(retstr,upper(s))>0) then
  61. procinfo.funcret_is_valid:=true;
  62. s:='';
  63. end;
  64. begin
  65. ende:=false;
  66. s:='';
  67. if assigned(procinfo.retdef) and
  68. is_fpu(procinfo.retdef) then
  69. procinfo.funcret_is_valid:=true;
  70. if assigned(procinfo.retdef) and
  71. (procinfo.retdef<>pdef(voiddef)) then
  72. retstr:=upper(tostr(procinfo.retoffset)+'('+att_reg2str[procinfo.framepointer]+')')
  73. else
  74. retstr:='';
  75. c:=current_scanner^.asmgetchar;
  76. code:=new(paasmoutput,init);
  77. while not(ende) do
  78. begin
  79. { wrong placement
  80. current_scanner^.gettokenpos; }
  81. case c of
  82. 'A'..'Z','a'..'z','_' : begin
  83. current_scanner^.gettokenpos;
  84. i:=0;
  85. hs:='';
  86. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  87. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  88. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  89. or (c='_') do
  90. begin
  91. inc(i);
  92. hs[i]:=c;
  93. c:=current_scanner^.asmgetchar;
  94. end;
  95. {$ifndef TP}
  96. {$ifopt H+}
  97. setlength(hs,i);
  98. {$else}
  99. hs[0]:=chr(i);
  100. {$endif}
  101. {$else}
  102. hs[0]:=chr(i);
  103. {$endif}
  104. if upper(hs)='END' then
  105. ende:=true
  106. else
  107. begin
  108. if c=':' then
  109. begin
  110. getsym(upper(hs),false);
  111. if srsym<>nil then
  112. if (srsym^.typ = labelsym) then
  113. Begin
  114. hs:=lab2str(plabelsym(srsym)^.number);
  115. {label is set !! }
  116. plabelsym(srsym)^.number^.is_set:=true;
  117. end
  118. else
  119. Message(assem_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(aktprocsym) then
  126. begin
  127. { is the last written character an special }
  128. { char ? }
  129. if (s[length(s)]='%') and
  130. ret_in_acc(procinfo.retdef) and
  131. ((pos('AX',upper(hs))>0) or
  132. (pos('AL',upper(hs))>0)) then
  133. procinfo.funcret_is_valid:=true;
  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(aktprocsym^.definition^.localst) and
  139. (lexlevel >= normal_function_level) then
  140. sym:=aktprocsym^.definition^.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:=lab2str(plabelsym(sym)^.number);
  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. pvarsym(sym)^.is_valid:=1
  155. else
  156. if (pos('MOV',upper(s)) > 0) and (pvarsym(sym)^.is_valid=0) then
  157. Message1(sym_n_uninitialized_local_variable,hs);
  158. hs:='-'+tostr(pvarsym(sym)^.address)+'('+att_reg2str[procinfo.framepointer]+')';
  159. end
  160. else
  161. { call to local function }
  162. if (sym^.typ=procsym) and ((pos('CALL',upper(s))>0) or
  163. (pos('LEA',upper(s))>0)) then
  164. begin
  165. hs:=pprocsym(sym)^.definition^.mangledname;
  166. end;
  167. end
  168. else
  169. begin
  170. if assigned(aktprocsym^.definition^.parast) then
  171. sym:=aktprocsym^.definition^.parast^.search(upper(hs))
  172. else
  173. sym:=nil;
  174. if assigned(sym) then
  175. begin
  176. if sym^.typ=varsym then
  177. begin
  178. l:=pvarsym(sym)^.address;
  179. { set offset }
  180. inc(l,aktprocsym^.definition^.parast^.call_offset);
  181. hs:=tostr(l)+'('+att_reg2str[procinfo.framepointer]+')';
  182. if pos(',',s) > 0 then
  183. pvarsym(sym)^.is_valid:=1;
  184. end;
  185. end
  186. { I added that but it creates a problem in line.ppi
  187. because there is a local label wbuffer and
  188. a static variable WBUFFER ...
  189. what would you decide, florian ?}
  190. else
  191. begin
  192. {$ifndef IGNOREGLOBALVAR}
  193. getsym(upper(hs),false);
  194. sym:=srsym;
  195. if assigned(sym) and (sym^.owner^.symtabletype in [unitsymtable,
  196. globalsymtable,staticsymtable]) then
  197. begin
  198. if (sym^.typ = varsym) or (sym^.typ = typedconstsym) then
  199. begin
  200. Do_comment(V_Warning,hs+' translated to '+sym^.mangledname);
  201. hs:=sym^.mangledname;
  202. end;
  203. { procs can be called or the address can be loaded }
  204. if (sym^.typ=procsym) and ((pos('CALL',upper(s))>0) or
  205. (pos('LEA',upper(s))>0)) then
  206. begin
  207. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  208. begin
  209. Do_comment(V_Warning,hs+' is associated to an overloaded function');
  210. end;
  211. Do_comment(V_Warning,hs+' translated to '+sym^.mangledname);
  212. hs:=sym^.mangledname;
  213. end;
  214. end
  215. else
  216. {$endif TESTGLOBALVAR}
  217. if upper(hs)='__SELF' then
  218. begin
  219. if assigned(procinfo._class) then
  220. hs:=tostr(procinfo.ESI_offset)+'('+att_reg2str[procinfo.framepointer]+')'
  221. else
  222. Message(assem_e_cannot_use_SELF_outside_a_method);
  223. end
  224. else if upper(hs)='__RESULT' then
  225. begin
  226. if assigned(procinfo.retdef) and
  227. (procinfo.retdef<>pdef(voiddef)) then
  228. begin
  229. hs:=retstr;
  230. end
  231. else
  232. Message(assem_w_void_function);
  233. end
  234. else if upper(hs)='__OLDEBP' then
  235. begin
  236. { complicate to check there }
  237. { we do it: }
  238. if lexlevel>normal_function_level then
  239. hs:=tostr(procinfo.framepointer_offset)
  240. +'('+att_reg2str[procinfo.framepointer]+')'
  241. else
  242. Message(assem_e_cannot_use___OLDEBP_outside_nested_procedure);
  243. end;
  244. end;
  245. end;
  246. end;
  247. end;
  248. s:=s+hs;
  249. end;
  250. end;
  251. '{',';',#10,#13 : begin
  252. if pos(retstr,s) > 0 then
  253. procinfo.funcret_is_valid:=true;
  254. writeasmline;
  255. c:=current_scanner^.asmgetchar;
  256. end;
  257. #26 : Message(scan_f_end_of_file);
  258. else
  259. begin
  260. current_scanner^.gettokenpos;
  261. {$ifndef TP}
  262. {$ifopt H+}
  263. setlength(s,length(s)+1);
  264. {$else}
  265. inc(byte(s[0]));
  266. {$endif}
  267. {$else}
  268. inc(byte(s[0]));
  269. {$endif}
  270. s[length(s)]:=c;
  271. c:=current_scanner^.asmgetchar;
  272. end;
  273. end;
  274. end;
  275. writeasmline;
  276. assemble:=genasmnode(code);
  277. end;
  278. end.
  279. {
  280. $Log$
  281. Revision 1.14 1999-02-22 02:15:36 peter
  282. * updates for ag386bin
  283. Revision 1.13 1999/01/27 13:04:12 pierre
  284. * bug with static vars in assembler readers
  285. Revision 1.12 1999/01/10 15:37:57 peter
  286. * moved some tables from ra386*.pas -> i386.pas
  287. + start of coff writer
  288. * renamed asmutils unit to rautils
  289. Revision 1.11 1998/11/17 00:26:12 peter
  290. * fixed for $H+
  291. Revision 1.10 1998/11/13 15:40:28 pierre
  292. + added -Se in Makefile cvstest target
  293. + lexlevel cleanup
  294. normal_function_level main_program_level and unit_init_level defined
  295. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  296. (test added in code !)
  297. * -Un option was wrong
  298. * _FAIL and _SELF only keyword inside
  299. constructors and methods respectively
  300. Revision 1.9 1998/10/20 08:06:57 pierre
  301. * several memory corruptions due to double freemem solved
  302. => never use p^.loc.location:=p^.left^.loc.location;
  303. + finally I added now by default
  304. that ra386dir translates global and unit symbols
  305. + added a first field in tsymtable and
  306. a nextsym field in tsym
  307. (this allows to obtain ordered type info for
  308. records and objects in gdb !)
  309. Revision 1.8 1998/09/04 08:42:08 peter
  310. * updated some error messages
  311. Revision 1.7 1998/09/03 17:39:05 florian
  312. + better code for type conversation longint/dword to real type
  313. Revision 1.6 1998/09/03 17:08:47 pierre
  314. * better lines for stabs
  315. (no scroll back to if before else part
  316. no return to case line at jump outside case)
  317. + source lines also if not in order
  318. Revision 1.5 1998/08/21 08:45:51 pierre
  319. * better line info for asm statements
  320. Revision 1.4 1998/07/14 14:46:59 peter
  321. * released NEWINPUT
  322. Revision 1.3 1998/07/07 11:20:08 peter
  323. + NEWINPUT for a better inputfile and scanner object
  324. Revision 1.2 1998/06/24 14:06:37 peter
  325. * fixed the name changes
  326. Revision 1.1 1998/06/23 14:00:18 peter
  327. * renamed RA* units
  328. Revision 1.5 1998/06/12 10:32:32 pierre
  329. * column problem hopefully solved
  330. + C vars declaration changed
  331. Revision 1.4 1998/06/04 23:51:58 peter
  332. * m68k compiles
  333. + .def file creation moved to gendef.pas so it could also be used
  334. for win32
  335. }