ra386dir.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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) and
  134. (lexlevel >= normal_function_level) then
  135. sym:=aktprocsym^.definition^.localst^.search(upper(hs))
  136. else
  137. sym:=nil;
  138. if assigned(sym) then
  139. begin
  140. if (sym^.typ = labelsym) then
  141. Begin
  142. hs:=lab2str(plabelsym(sym)^.number);
  143. end
  144. else if sym^.typ=varsym then
  145. begin
  146. {variables set are after a comma }
  147. {like in movl %eax,I }
  148. if pos(',',s) > 0 then
  149. pvarsym(sym)^.is_valid:=1
  150. else
  151. if (pos('MOV',upper(s)) > 0) and (pvarsym(sym)^.is_valid=0) then
  152. Message1(sym_n_uninitialized_local_variable,hs);
  153. hs:='-'+tostr(pvarsym(sym)^.address)+'('+att_reg2str[procinfo.framepointer]+')';
  154. end
  155. else
  156. { call to local function }
  157. if (sym^.typ=procsym) and ((pos('CALL',upper(s))>0) or
  158. (pos('LEA',upper(s))>0)) then
  159. begin
  160. hs:=pprocsym(sym)^.definition^.mangledname;
  161. end;
  162. end
  163. else
  164. begin
  165. if assigned(aktprocsym^.definition^.parast) then
  166. sym:=aktprocsym^.definition^.parast^.search(upper(hs))
  167. else
  168. sym:=nil;
  169. if assigned(sym) then
  170. begin
  171. if sym^.typ=varsym then
  172. begin
  173. l:=pvarsym(sym)^.address;
  174. { set offset }
  175. inc(l,aktprocsym^.definition^.parast^.call_offset);
  176. hs:=tostr(l)+'('+att_reg2str[procinfo.framepointer]+')';
  177. if pos(',',s) > 0 then
  178. pvarsym(sym)^.is_valid:=1;
  179. end;
  180. end
  181. { I added that but it creates a problem in line.ppi
  182. because there is a local label wbuffer and
  183. a static variable WBUFFER ...
  184. what would you decide, florian ?}
  185. else
  186. begin
  187. {$ifndef IGNOREGLOBALVAR}
  188. getsym(upper(hs),false);
  189. sym:=srsym;
  190. if assigned(sym) and (sym^.owner^.symtabletype in [unitsymtable,
  191. globalsymtable,staticsymtable]) then
  192. begin
  193. if (sym^.typ = varsym) or (sym^.typ = typedconstsym) then
  194. begin
  195. Do_comment(V_Warning,hs+' translated to '+sym^.mangledname);
  196. hs:=sym^.mangledname;
  197. end;
  198. { procs can be called or the address can be loaded }
  199. if (sym^.typ=procsym) and ((pos('CALL',upper(s))>0) or
  200. (pos('LEA',upper(s))>0)) then
  201. begin
  202. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  203. begin
  204. Do_comment(V_Warning,hs+' is associated to an overloaded function');
  205. end;
  206. Do_comment(V_Warning,hs+' translated to '+sym^.mangledname);
  207. hs:=sym^.mangledname;
  208. end;
  209. end
  210. else
  211. {$endif TESTGLOBALVAR}
  212. if upper(hs)='__SELF' then
  213. begin
  214. if assigned(procinfo._class) then
  215. hs:=tostr(procinfo.ESI_offset)+'('+att_reg2str[procinfo.framepointer]+')'
  216. else
  217. Message(assem_e_cannot_use_SELF_outside_a_method);
  218. end
  219. else if upper(hs)='__RESULT' then
  220. begin
  221. if assigned(procinfo.retdef) and
  222. (procinfo.retdef<>pdef(voiddef)) then
  223. begin
  224. hs:=retstr;
  225. end
  226. else
  227. Message(assem_w_void_function);
  228. end
  229. else if upper(hs)='__OLDEBP' then
  230. begin
  231. { complicate to check there }
  232. { we do it: }
  233. if lexlevel>normal_function_level then
  234. hs:=tostr(procinfo.framepointer_offset)
  235. +'('+att_reg2str[procinfo.framepointer]+')'
  236. else
  237. Message(assem_e_cannot_use___OLDEBP_outside_nested_procedure);
  238. end;
  239. end;
  240. end;
  241. end;
  242. end;
  243. s:=s+hs;
  244. end;
  245. end;
  246. '{',';',#10,#13 : begin
  247. if pos(retstr,s) > 0 then
  248. procinfo.funcret_is_valid:=true;
  249. writeasmline;
  250. c:=current_scanner^.asmgetchar;
  251. end;
  252. #26 : Message(scan_f_end_of_file);
  253. else
  254. begin
  255. current_scanner^.gettokenpos;
  256. {$ifndef TP}
  257. {$ifopt H+}
  258. setlength(s,length(s)+1);
  259. {$else}
  260. inc(byte(s[0]));
  261. {$endif}
  262. {$else}
  263. inc(byte(s[0]));
  264. {$endif}
  265. s[length(s)]:=c;
  266. c:=current_scanner^.asmgetchar;
  267. end;
  268. end;
  269. end;
  270. writeasmline;
  271. assemble:=genasmnode(code);
  272. end;
  273. end.
  274. {
  275. $Log$
  276. Revision 1.13 1999-01-27 13:04:12 pierre
  277. * bug with static vars in assembler readers
  278. Revision 1.12 1999/01/10 15:37:57 peter
  279. * moved some tables from ra386*.pas -> i386.pas
  280. + start of coff writer
  281. * renamed asmutils unit to rautils
  282. Revision 1.11 1998/11/17 00:26:12 peter
  283. * fixed for $H+
  284. Revision 1.10 1998/11/13 15:40:28 pierre
  285. + added -Se in Makefile cvstest target
  286. + lexlevel cleanup
  287. normal_function_level main_program_level and unit_init_level defined
  288. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  289. (test added in code !)
  290. * -Un option was wrong
  291. * _FAIL and _SELF only keyword inside
  292. constructors and methods respectively
  293. Revision 1.9 1998/10/20 08:06:57 pierre
  294. * several memory corruptions due to double freemem solved
  295. => never use p^.loc.location:=p^.left^.loc.location;
  296. + finally I added now by default
  297. that ra386dir translates global and unit symbols
  298. + added a first field in tsymtable and
  299. a nextsym field in tsym
  300. (this allows to obtain ordered type info for
  301. records and objects in gdb !)
  302. Revision 1.8 1998/09/04 08:42:08 peter
  303. * updated some error messages
  304. Revision 1.7 1998/09/03 17:39:05 florian
  305. + better code for type conversation longint/dword to real type
  306. Revision 1.6 1998/09/03 17:08:47 pierre
  307. * better lines for stabs
  308. (no scroll back to if before else part
  309. no return to case line at jump outside case)
  310. + source lines also if not in order
  311. Revision 1.5 1998/08/21 08:45:51 pierre
  312. * better line info for asm statements
  313. Revision 1.4 1998/07/14 14:46:59 peter
  314. * released NEWINPUT
  315. Revision 1.3 1998/07/07 11:20:08 peter
  316. + NEWINPUT for a better inputfile and scanner object
  317. Revision 1.2 1998/06/24 14:06:37 peter
  318. * fixed the name changes
  319. Revision 1.1 1998/06/23 14:00:18 peter
  320. * renamed RA* units
  321. Revision 1.5 1998/06/12 10:32:32 pierre
  322. * column problem hopefully solved
  323. + C vars declaration changed
  324. Revision 1.4 1998/06/04 23:51:58 peter
  325. * m68k compiles
  326. + .def file creation moved to gendef.pas so it could also be used
  327. for win32
  328. }