radi386.pas 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 radi386;
  19. interface
  20. uses
  21. tree;
  22. function assemble : ptree;
  23. implementation
  24. uses
  25. 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. { if function return is param }
  46. { consider it set if the offset was loaded }
  47. if assigned(procinfo.retdef) and
  48. ret_in_param(procinfo.retdef) and
  49. (pos(retstr,upper(s))>0) then
  50. procinfo.funcret_is_valid:=true;
  51. s:='';
  52. end;
  53. begin
  54. ende:=false;
  55. s:='';
  56. if assigned(procinfo.retdef) and
  57. (procinfo.retdef<>pdef(voiddef)) then
  58. retstr:=upper(tostr(procinfo.retoffset)+'('+att_reg2str[procinfo.framepointer]+')')
  59. else
  60. retstr:='';
  61. c:=asmgetchar;
  62. code:=new(paasmoutput,init);
  63. while not(ende) do
  64. begin
  65. case c of
  66. 'A'..'Z','a'..'z','_' : begin
  67. hs:='';
  68. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  69. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  70. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  71. or (c='_') do
  72. begin
  73. inc(byte(hs[0]));
  74. hs[length(hs)]:=c;
  75. c:=asmgetchar;
  76. end;
  77. if upper(hs)='END' then
  78. ende:=true
  79. else
  80. begin
  81. if c=':' then
  82. begin
  83. getsym(upper(hs),false);
  84. if srsym<>nil then
  85. Message(assem_w_using_defined_as_local);
  86. end;
  87. if upper(hs)='FWAIT' then
  88. FwaitWarning
  89. else
  90. { access to local variables }
  91. if assigned(aktprocsym) then
  92. begin
  93. { is the last written character an special }
  94. { char ? }
  95. if (s[length(s)]<>'%') and
  96. (s[length(s)]<>'$') then
  97. begin
  98. if assigned(aktprocsym^.definition^.localst) then
  99. sym:=aktprocsym^.definition^.localst^.search(upper(hs))
  100. else
  101. sym:=nil;
  102. if assigned(sym) then
  103. begin
  104. if sym^.typ=varsym then
  105. begin
  106. {variables set are after a comma }
  107. {like in movl %eax,I }
  108. if pos(',',s) > 0 then
  109. pvarsym(sym)^.is_valid:=1
  110. else
  111. if (pos('MOV',upper(s)) > 0) and (pvarsym(sym)^.is_valid=0) then
  112. Message1(sym_n_local_var_not_init_yet,hs);
  113. hs:='-'+tostr(pvarsym(sym)^.address)+'('+att_reg2str[procinfo.framepointer]+')';
  114. end
  115. else
  116. { call to local function }
  117. if (sym^.typ=procsym) and (pos('CALL',upper(s))>0) then
  118. begin
  119. hs:=pprocsym(sym)^.definition^.mangledname;
  120. end;
  121. end
  122. else
  123. begin
  124. if assigned(aktprocsym^.definition^.parast) then
  125. sym:=aktprocsym^.definition^.parast^.search(upper(hs))
  126. else
  127. sym:=nil;
  128. if assigned(sym) then
  129. begin
  130. if sym^.typ=varsym then
  131. begin
  132. l:=pvarsym(sym)^.address;
  133. { set offset }
  134. inc(l,aktprocsym^.definition^.parast^.call_offset);
  135. hs:=tostr(l)+'('+att_reg2str[procinfo.framepointer]+')';
  136. if pos(',',s) > 0 then
  137. pvarsym(sym)^.is_valid:=1;
  138. end;
  139. end
  140. { I added that but it creates a problem in line.ppi
  141. because there is a local label wbuffer and
  142. a static variable WBUFFER ...
  143. what would you decide, florian ?
  144. else
  145. begin
  146. getsym(upper(hs),false);
  147. sym:=srsym;
  148. if assigned(sym) and (sym^.typ = varsym)
  149. or (sym^.typ = typedconstsym) then
  150. hs:=sym^.mangledname;
  151. if (sym^.typ=procsym) and (pos('CALL',upper(s))>0) then
  152. begin
  153. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  154. begin
  155. exterror:=strpnew(' calling an overloaded procedure in asm');
  156. warning(user_defined);
  157. end;
  158. hs:=pprocsym(sym)^.definition^.mangledname;
  159. end;
  160. end }
  161. else if upper(hs)='__SELF' then
  162. begin
  163. if assigned(procinfo._class) then
  164. hs:=tostr(procinfo.ESI_offset)+'('+att_reg2str[procinfo.framepointer]+')'
  165. else
  166. Message(assem_e_cannot_use_SELF_outside_a_method);
  167. end
  168. else if upper(hs)='__RESULT' then
  169. begin
  170. if assigned(procinfo.retdef) and
  171. (procinfo.retdef<>pdef(voiddef)) then
  172. begin
  173. hs:=retstr;
  174. if pos(',',s) > 0 then
  175. procinfo.funcret_is_valid:=true;
  176. end
  177. else
  178. Message(assem_w_void_function);
  179. end
  180. else if upper(hs)='__OLDEBP' then
  181. begin
  182. { complicate to check there }
  183. { we do it: }
  184. if lexlevel>2 then
  185. hs:=tostr(procinfo.framepointer_offset)
  186. +'('+att_reg2str[procinfo.framepointer]+')'
  187. else
  188. Message(assem_e_cannot_use___OLDEBP_outside_nested_procedure);
  189. end;
  190. end;
  191. { end;}
  192. end;
  193. end;
  194. s:=s+hs;
  195. end;
  196. end;
  197. '{',';',#10,#13 : begin
  198. writeasmline;
  199. c:=asmgetchar;
  200. end;
  201. #26 : Message(scan_f_end_of_file);
  202. else
  203. begin
  204. inc(byte(s[0]));
  205. s[length(s)]:=c;
  206. c:=asmgetchar;
  207. end;
  208. end;
  209. end;
  210. writeasmline;
  211. assemble:=genasmnode(code);
  212. end;
  213. end.
  214. {
  215. $Log$
  216. Revision 1.1 1998-03-25 11:18:15 root
  217. Initial revision
  218. Revision 1.13 1998/03/24 21:48:33 florian
  219. * just a couple of fixes applied:
  220. - problem with fixed16 solved
  221. - internalerror 10005 problem fixed
  222. - patch for assembler reading
  223. - small optimizer fix
  224. - mem is now supported
  225. Revision 1.12 1998/03/10 16:27:43 pierre
  226. * better line info in stabs debug
  227. * symtabletype and lexlevel separated into two fields of tsymtable
  228. + ifdef MAKELIB for direct library output, not complete
  229. + ifdef CHAINPROCSYMS for overloaded seach across units, not fully
  230. working
  231. + ifdef TESTFUNCRET for setting func result in underfunction, not
  232. working
  233. Revision 1.11 1998/03/10 01:17:26 peter
  234. * all files have the same header
  235. * messages are fully implemented, EXTDEBUG uses Comment()
  236. + AG... files for the Assembler generation
  237. Revision 1.10 1998/03/09 12:58:12 peter
  238. * FWait warning is only showed for Go32V2 and $E+
  239. * opcode tables moved to i386.pas/m68k.pas to reduce circular uses (and
  240. for m68k the same tables are removed)
  241. + $E for i386
  242. Revision 1.9 1998/03/06 00:52:51 peter
  243. * replaced all old messages from errore.msg, only ExtDebug and some
  244. Comment() calls are left
  245. * fixed options.pas
  246. Revision 1.8 1998/03/03 16:45:23 peter
  247. + message support for assembler parsers
  248. Revision 1.7 1998/03/02 01:49:14 peter
  249. * renamed target_DOS to target_GO32V1
  250. + new verbose system, merged old errors and verbose units into one new
  251. verbose.pas, so errors.pas is obsolete
  252. Revision 1.6 1998/02/13 10:35:35 daniel
  253. * Made Motorola version compilable.
  254. * Fixed optimizer
  255. Revision 1.5 1998/02/07 18:01:27 carl
  256. + fwait warning for emulation
  257. Revision 1.3 1997/11/30 18:12:17 carl
  258. * bugfix of line numbering.
  259. Revision 1.2 1997/11/28 18:14:44 pierre
  260. working version with several bug fixes
  261. Revision 1.1.1.1 1997/11/27 08:33:00 michael
  262. FPC Compiler CVS start
  263. Pre-CVS log:
  264. History:
  265. 19th october 1996:
  266. + created from old asmbl.pas
  267. 13th october 1996:
  268. + renamed to radi386
  269. }