ra386dir.pas 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. 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. current_scanner^.gettokenpos;
  67. case c of
  68. 'A'..'Z','a'..'z','_' : begin
  69. hs:='';
  70. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  71. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  72. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  73. or (c='_') do
  74. begin
  75. inc(byte(hs[0]));
  76. hs[length(hs)]:=c;
  77. c:=current_scanner^.asmgetchar;
  78. end;
  79. if upper(hs)='END' then
  80. ende:=true
  81. else
  82. begin
  83. if c=':' then
  84. begin
  85. getsym(upper(hs),false);
  86. if srsym<>nil then
  87. Message(assem_w_using_defined_as_local);
  88. end;
  89. if upper(hs)='FWAIT' then
  90. FwaitWarning
  91. else
  92. { access to local variables }
  93. if assigned(aktprocsym) then
  94. begin
  95. { is the last written character an special }
  96. { char ? }
  97. if (s[length(s)]='%') and
  98. ret_in_acc(procinfo.retdef) and
  99. ((pos('AX',upper(hs))>0) or
  100. (pos('AL',upper(hs))>0)) then
  101. procinfo.funcret_is_valid:=true;
  102. if (s[length(s)]<>'%') and
  103. (s[length(s)]<>'$') and
  104. ((s[length(s)]<>'0') or (hs[1]<>'x')) then
  105. begin
  106. if assigned(aktprocsym^.definition^.localst) then
  107. sym:=aktprocsym^.definition^.localst^.search(upper(hs))
  108. else
  109. sym:=nil;
  110. if assigned(sym) then
  111. begin
  112. if sym^.typ=varsym then
  113. begin
  114. {variables set are after a comma }
  115. {like in movl %eax,I }
  116. if pos(',',s) > 0 then
  117. pvarsym(sym)^.is_valid:=1
  118. else
  119. if (pos('MOV',upper(s)) > 0) and (pvarsym(sym)^.is_valid=0) then
  120. Message1(sym_n_local_var_not_init_yet,hs);
  121. hs:='-'+tostr(pvarsym(sym)^.address)+'('+att_reg2str[procinfo.framepointer]+')';
  122. end
  123. else
  124. { call to local function }
  125. if (sym^.typ=procsym) and (pos('CALL',upper(s))>0) then
  126. begin
  127. hs:=pprocsym(sym)^.definition^.mangledname;
  128. end;
  129. end
  130. else
  131. begin
  132. if assigned(aktprocsym^.definition^.parast) then
  133. sym:=aktprocsym^.definition^.parast^.search(upper(hs))
  134. else
  135. sym:=nil;
  136. if assigned(sym) then
  137. begin
  138. if sym^.typ=varsym then
  139. begin
  140. l:=pvarsym(sym)^.address;
  141. { set offset }
  142. inc(l,aktprocsym^.definition^.parast^.call_offset);
  143. hs:=tostr(l)+'('+att_reg2str[procinfo.framepointer]+')';
  144. if pos(',',s) > 0 then
  145. pvarsym(sym)^.is_valid:=1;
  146. end;
  147. end
  148. { I added that but it creates a problem in line.ppi
  149. because there is a local label wbuffer and
  150. a static variable WBUFFER ...
  151. what would you decide, florian ?
  152. else
  153. begin
  154. getsym(upper(hs),false);
  155. sym:=srsym;
  156. if assigned(sym) and (sym^.typ = varsym)
  157. or (sym^.typ = typedconstsym) then
  158. hs:=sym^.mangledname;
  159. if (sym^.typ=procsym) and (pos('CALL',upper(s))>0) then
  160. begin
  161. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  162. begin
  163. exterror:=strpnew(' calling an overloaded procedure in asm');
  164. warning(user_defined);
  165. end;
  166. hs:=pprocsym(sym)^.definition^.mangledname;
  167. end;
  168. end }
  169. else if upper(hs)='__SELF' then
  170. begin
  171. if assigned(procinfo._class) then
  172. hs:=tostr(procinfo.ESI_offset)+'('+att_reg2str[procinfo.framepointer]+')'
  173. else
  174. Message(assem_e_cannot_use_SELF_outside_a_method);
  175. end
  176. else if upper(hs)='__RESULT' then
  177. begin
  178. if assigned(procinfo.retdef) and
  179. (procinfo.retdef<>pdef(voiddef)) then
  180. begin
  181. hs:=retstr;
  182. end
  183. else
  184. Message(assem_w_void_function);
  185. end
  186. else if upper(hs)='__OLDEBP' then
  187. begin
  188. { complicate to check there }
  189. { we do it: }
  190. if lexlevel>2 then
  191. hs:=tostr(procinfo.framepointer_offset)
  192. +'('+att_reg2str[procinfo.framepointer]+')'
  193. else
  194. Message(assem_e_cannot_use___OLDEBP_outside_nested_procedure);
  195. end;
  196. end;
  197. { end;}
  198. end;
  199. end;
  200. s:=s+hs;
  201. end;
  202. end;
  203. '{',';',#10,#13 : begin
  204. if pos(retstr,s) > 0 then
  205. procinfo.funcret_is_valid:=true;
  206. writeasmline;
  207. c:=current_scanner^.asmgetchar;
  208. end;
  209. #26 : Message(scan_f_end_of_file);
  210. else
  211. begin
  212. inc(byte(s[0]));
  213. s[length(s)]:=c;
  214. c:=current_scanner^.asmgetchar;
  215. end;
  216. end;
  217. end;
  218. writeasmline;
  219. assemble:=genasmnode(code);
  220. end;
  221. end.
  222. {
  223. $Log$
  224. Revision 1.4 1998-07-14 14:46:59 peter
  225. * released NEWINPUT
  226. Revision 1.3 1998/07/07 11:20:08 peter
  227. + NEWINPUT for a better inputfile and scanner object
  228. Revision 1.2 1998/06/24 14:06:37 peter
  229. * fixed the name changes
  230. Revision 1.1 1998/06/23 14:00:18 peter
  231. * renamed RA* units
  232. Revision 1.5 1998/06/12 10:32:32 pierre
  233. * column problem hopefully solved
  234. + C vars declaration changed
  235. Revision 1.4 1998/06/04 23:51:58 peter
  236. * m68k compiles
  237. + .def file creation moved to gendef.pas so it could also be used
  238. for win32
  239. }