ra386dir.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. if sym^.typ=varsym then
  203. inc(pvarsym(sym)^.refs);
  204. end;
  205. { procs can be called or the address can be loaded }
  206. if (sym^.typ=procsym) and ((pos('CALL',upper(s))>0) or
  207. (pos('LEA',upper(s))>0)) then
  208. begin
  209. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  210. begin
  211. Do_comment(V_Warning,hs+' is associated to an overloaded function');
  212. end;
  213. Do_comment(V_Warning,hs+' translated to '+sym^.mangledname);
  214. hs:=sym^.mangledname;
  215. end;
  216. end
  217. else
  218. {$endif TESTGLOBALVAR}
  219. if upper(hs)='__SELF' then
  220. begin
  221. if assigned(procinfo._class) then
  222. hs:=tostr(procinfo.ESI_offset)+'('+att_reg2str[procinfo.framepointer]+')'
  223. else
  224. Message(assem_e_cannot_use_SELF_outside_a_method);
  225. end
  226. else if upper(hs)='__RESULT' then
  227. begin
  228. if assigned(procinfo.retdef) and
  229. (procinfo.retdef<>pdef(voiddef)) then
  230. begin
  231. hs:=retstr;
  232. end
  233. else
  234. Message(assem_w_void_function);
  235. end
  236. else if upper(hs)='__OLDEBP' then
  237. begin
  238. { complicate to check there }
  239. { we do it: }
  240. if lexlevel>normal_function_level then
  241. hs:=tostr(procinfo.framepointer_offset)
  242. +'('+att_reg2str[procinfo.framepointer]+')'
  243. else
  244. Message(assem_e_cannot_use___OLDEBP_outside_nested_procedure);
  245. end;
  246. end;
  247. end;
  248. end;
  249. end;
  250. s:=s+hs;
  251. end;
  252. end;
  253. '{',';',#10,#13 : begin
  254. if pos(retstr,s) > 0 then
  255. procinfo.funcret_is_valid:=true;
  256. writeasmline;
  257. c:=current_scanner^.asmgetchar;
  258. end;
  259. #26 : Message(scan_f_end_of_file);
  260. else
  261. begin
  262. current_scanner^.gettokenpos;
  263. {$ifndef TP}
  264. {$ifopt H+}
  265. setlength(s,length(s)+1);
  266. {$else}
  267. inc(byte(s[0]));
  268. {$endif}
  269. {$else}
  270. inc(byte(s[0]));
  271. {$endif}
  272. s[length(s)]:=c;
  273. c:=current_scanner^.asmgetchar;
  274. end;
  275. end;
  276. end;
  277. writeasmline;
  278. assemble:=genasmnode(code);
  279. end;
  280. end.
  281. {
  282. $Log$
  283. Revision 1.15 1999-03-01 13:22:26 pierre
  284. * varsym refs incremented
  285. Revision 1.14 1999/02/22 02:15:36 peter
  286. * updates for ag386bin
  287. Revision 1.13 1999/01/27 13:04:12 pierre
  288. * bug with static vars in assembler readers
  289. Revision 1.12 1999/01/10 15:37:57 peter
  290. * moved some tables from ra386*.pas -> i386.pas
  291. + start of coff writer
  292. * renamed asmutils unit to rautils
  293. Revision 1.11 1998/11/17 00:26:12 peter
  294. * fixed for $H+
  295. Revision 1.10 1998/11/13 15:40:28 pierre
  296. + added -Se in Makefile cvstest target
  297. + lexlevel cleanup
  298. normal_function_level main_program_level and unit_init_level defined
  299. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  300. (test added in code !)
  301. * -Un option was wrong
  302. * _FAIL and _SELF only keyword inside
  303. constructors and methods respectively
  304. Revision 1.9 1998/10/20 08:06:57 pierre
  305. * several memory corruptions due to double freemem solved
  306. => never use p^.loc.location:=p^.left^.loc.location;
  307. + finally I added now by default
  308. that ra386dir translates global and unit symbols
  309. + added a first field in tsymtable and
  310. a nextsym field in tsym
  311. (this allows to obtain ordered type info for
  312. records and objects in gdb !)
  313. Revision 1.8 1998/09/04 08:42:08 peter
  314. * updated some error messages
  315. Revision 1.7 1998/09/03 17:39:05 florian
  316. + better code for type conversation longint/dword to real type
  317. Revision 1.6 1998/09/03 17:08:47 pierre
  318. * better lines for stabs
  319. (no scroll back to if before else part
  320. no return to case line at jump outside case)
  321. + source lines also if not in order
  322. Revision 1.5 1998/08/21 08:45:51 pierre
  323. * better line info for asm statements
  324. Revision 1.4 1998/07/14 14:46:59 peter
  325. * released NEWINPUT
  326. Revision 1.3 1998/07/07 11:20:08 peter
  327. + NEWINPUT for a better inputfile and scanner object
  328. Revision 1.2 1998/06/24 14:06:37 peter
  329. * fixed the name changes
  330. Revision 1.1 1998/06/23 14:00:18 peter
  331. * renamed RA* units
  332. Revision 1.5 1998/06/12 10:32:32 pierre
  333. * column problem hopefully solved
  334. + C vars declaration changed
  335. Revision 1.4 1998/06/04 23:51:58 peter
  336. * m68k compiles
  337. + .def file creation moved to gendef.pas so it could also be used
  338. for win32
  339. }