ra386dir.pas 16 KB

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