ra386dir.pas 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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,asmutils;
  27. function assemble : ptree;
  28. var
  29. retstr,s,hs : string;
  30. c : char;
  31. ende : boolean;
  32. sym : psym;
  33. code : paasmoutput;
  34. 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. s[0]:=chr(i);
  43. if s<>'' then
  44. code^.concat(new(pai_direct,init(strpnew(s))));
  45. { consider it set function set if the offset was loaded }
  46. if assigned(procinfo.retdef) and
  47. (pos(retstr,upper(s))>0) then
  48. procinfo.funcret_is_valid:=true;
  49. s:='';
  50. end;
  51. begin
  52. ende:=false;
  53. s:='';
  54. if assigned(procinfo.retdef) and
  55. is_fpu(procinfo.retdef) then
  56. procinfo.funcret_is_valid:=true;
  57. if assigned(procinfo.retdef) and
  58. (procinfo.retdef<>pdef(voiddef)) then
  59. retstr:=upper(tostr(procinfo.retoffset)+'('+att_reg2str[procinfo.framepointer]+')')
  60. else
  61. retstr:='';
  62. c:=current_scanner^.asmgetchar;
  63. code:=new(paasmoutput,init);
  64. while not(ende) do
  65. begin
  66. { wrong placement
  67. current_scanner^.gettokenpos; }
  68. case c of
  69. 'A'..'Z','a'..'z','_' : begin
  70. hs:='';
  71. current_scanner^.gettokenpos;
  72. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  73. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  74. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  75. or (c='_') do
  76. begin
  77. inc(byte(hs[0]));
  78. hs[length(hs)]:=c;
  79. c:=current_scanner^.asmgetchar;
  80. end;
  81. if upper(hs)='END' then
  82. ende:=true
  83. else
  84. begin
  85. if c=':' then
  86. begin
  87. getsym(upper(hs),false);
  88. if srsym<>nil then
  89. if (srsym^.typ = labelsym) then
  90. Begin
  91. hs:=lab2str(plabelsym(srsym)^.number);
  92. {label is set !! }
  93. plabelsym(srsym)^.number^.is_set:=true;
  94. end
  95. else
  96. Message(assem_w_using_defined_as_local);
  97. end
  98. else if upper(hs)='FWAIT' then
  99. FwaitWarning
  100. else
  101. { access to local variables }
  102. if assigned(aktprocsym) then
  103. begin
  104. { is the last written character an special }
  105. { char ? }
  106. if (s[length(s)]='%') and
  107. ret_in_acc(procinfo.retdef) and
  108. ((pos('AX',upper(hs))>0) or
  109. (pos('AL',upper(hs))>0)) then
  110. procinfo.funcret_is_valid:=true;
  111. if (s[length(s)]<>'%') and
  112. (s[length(s)]<>'$') and
  113. ((s[length(s)]<>'0') or (hs[1]<>'x')) then
  114. begin
  115. if assigned(aktprocsym^.definition^.localst) then
  116. sym:=aktprocsym^.definition^.localst^.search(upper(hs))
  117. else
  118. sym:=nil;
  119. if assigned(sym) then
  120. begin
  121. if (sym^.typ = labelsym) then
  122. Begin
  123. hs:=lab2str(plabelsym(sym)^.number);
  124. end
  125. else if sym^.typ=varsym then
  126. begin
  127. {variables set are after a comma }
  128. {like in movl %eax,I }
  129. if pos(',',s) > 0 then
  130. pvarsym(sym)^.is_valid:=1
  131. else
  132. if (pos('MOV',upper(s)) > 0) and (pvarsym(sym)^.is_valid=0) then
  133. Message1(sym_n_uninitialized_local_variable,hs);
  134. hs:='-'+tostr(pvarsym(sym)^.address)+'('+att_reg2str[procinfo.framepointer]+')';
  135. end
  136. else
  137. { call to local function }
  138. if (sym^.typ=procsym) and ((pos('CALL',upper(s))>0) or
  139. (pos('LEA',upper(s))>0)) then
  140. begin
  141. hs:=pprocsym(sym)^.definition^.mangledname;
  142. end;
  143. end
  144. else
  145. begin
  146. if assigned(aktprocsym^.definition^.parast) then
  147. sym:=aktprocsym^.definition^.parast^.search(upper(hs))
  148. else
  149. sym:=nil;
  150. if assigned(sym) then
  151. begin
  152. if sym^.typ=varsym then
  153. begin
  154. l:=pvarsym(sym)^.address;
  155. { set offset }
  156. inc(l,aktprocsym^.definition^.parast^.call_offset);
  157. hs:=tostr(l)+'('+att_reg2str[procinfo.framepointer]+')';
  158. if pos(',',s) > 0 then
  159. pvarsym(sym)^.is_valid:=1;
  160. end;
  161. end
  162. { I added that but it creates a problem in line.ppi
  163. because there is a local label wbuffer and
  164. a static variable WBUFFER ...
  165. what would you decide, florian ?}
  166. else
  167. begin
  168. {$ifdef TESTGLOBALVAR}
  169. getsym(upper(hs),false);
  170. sym:=srsym;
  171. if assigned(sym) and (sym^.owner^.symtabletype in [unitsymtable,
  172. globalsymtable,staticsymtable]) then
  173. begin
  174. if (sym^.typ = varsym) or (sym^.typ = typedconstsym) then
  175. begin
  176. Do_comment(V_Warning,hs+' translated to '+sym^.mangledname);
  177. hs:=sym^.mangledname;
  178. end;
  179. { procs can be called or the address can be loaded }
  180. if (sym^.typ=procsym) and ((pos('CALL',upper(s))>0) or
  181. (pos('LEA',upper(s))>0)) then
  182. begin
  183. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  184. begin
  185. Do_comment(V_Warning,hs+' is associated to an overloaded function');
  186. end;
  187. Do_comment(V_Warning,hs+' translated to '+sym^.mangledname);
  188. hs:=sym^.mangledname;
  189. end;
  190. end
  191. else
  192. {$endif TESTGLOBALVAR}
  193. if upper(hs)='__SELF' then
  194. begin
  195. if assigned(procinfo._class) then
  196. hs:=tostr(procinfo.ESI_offset)+'('+att_reg2str[procinfo.framepointer]+')'
  197. else
  198. Message(assem_e_cannot_use_SELF_outside_a_method);
  199. end
  200. else if upper(hs)='__RESULT' then
  201. begin
  202. if assigned(procinfo.retdef) and
  203. (procinfo.retdef<>pdef(voiddef)) then
  204. begin
  205. hs:=retstr;
  206. end
  207. else
  208. Message(assem_w_void_function);
  209. end
  210. else if upper(hs)='__OLDEBP' then
  211. begin
  212. { complicate to check there }
  213. { we do it: }
  214. if lexlevel>2 then
  215. hs:=tostr(procinfo.framepointer_offset)
  216. +'('+att_reg2str[procinfo.framepointer]+')'
  217. else
  218. Message(assem_e_cannot_use___OLDEBP_outside_nested_procedure);
  219. end;
  220. end;
  221. end;
  222. end;
  223. end;
  224. s:=s+hs;
  225. end;
  226. end;
  227. '{',';',#10,#13 : begin
  228. if pos(retstr,s) > 0 then
  229. procinfo.funcret_is_valid:=true;
  230. writeasmline;
  231. c:=current_scanner^.asmgetchar;
  232. end;
  233. #26 : Message(scan_f_end_of_file);
  234. else
  235. begin
  236. current_scanner^.gettokenpos;
  237. inc(byte(s[0]));
  238. s[length(s)]:=c;
  239. c:=current_scanner^.asmgetchar;
  240. end;
  241. end;
  242. end;
  243. writeasmline;
  244. assemble:=genasmnode(code);
  245. end;
  246. end.
  247. {
  248. $Log$
  249. Revision 1.8 1998-09-04 08:42:08 peter
  250. * updated some error messages
  251. Revision 1.7 1998/09/03 17:39:05 florian
  252. + better code for type conversation longint/dword to real type
  253. Revision 1.6 1998/09/03 17:08:47 pierre
  254. * better lines for stabs
  255. (no scroll back to if before else part
  256. no return to case line at jump outside case)
  257. + source lines also if not in order
  258. Revision 1.5 1998/08/21 08:45:51 pierre
  259. * better line info for asm statements
  260. Revision 1.4 1998/07/14 14:46:59 peter
  261. * released NEWINPUT
  262. Revision 1.3 1998/07/07 11:20:08 peter
  263. + NEWINPUT for a better inputfile and scanner object
  264. Revision 1.2 1998/06/24 14:06:37 peter
  265. * fixed the name changes
  266. Revision 1.1 1998/06/23 14:00:18 peter
  267. * renamed RA* units
  268. Revision 1.5 1998/06/12 10:32:32 pierre
  269. * column problem hopefully solved
  270. + C vars declaration changed
  271. Revision 1.4 1998/06/04 23:51:58 peter
  272. * m68k compiles
  273. + .def file creation moved to gendef.pas so it could also be used
  274. for win32
  275. }