ra386dir.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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,globals,scanner,aasm,cpubase,cpuasm,
  26. cobjects,symconst,symtable,types,verbose,
  27. {$ifdef NEWCG}
  28. cgbase,
  29. {$else}
  30. hcodegen,
  31. {$endif}
  32. rautils,ra386;
  33. function assemble : ptree;
  34. var
  35. retstr,s,hs : string;
  36. c : char;
  37. ende : boolean;
  38. sym : psym;
  39. code : paasmoutput;
  40. i,l : longint;
  41. procedure writeasmline;
  42. var
  43. i : longint;
  44. begin
  45. i:=length(s);
  46. while (i>0) and (s[i] in [' ',#9]) do
  47. dec(i);
  48. {$ifndef TP}
  49. {$ifopt H+}
  50. setlength(s,i);
  51. {$else}
  52. s[0]:=chr(i);
  53. {$endif}
  54. {$else}
  55. s[0]:=chr(i);
  56. {$endif}
  57. if s<>'' then
  58. code^.concat(new(pai_direct,init(strpnew(s))));
  59. { consider it set function set if the offset was loaded }
  60. if assigned(procinfo^.returntype.def) and
  61. (pos(retstr,upper(s))>0) then
  62. procinfo^.funcret_state:=vs_assigned;
  63. s:='';
  64. end;
  65. begin
  66. ende:=false;
  67. s:='';
  68. if assigned(procinfo^.returntype.def) and
  69. is_fpu(procinfo^.returntype.def) then
  70. procinfo^.funcret_state:=vs_assigned;
  71. if assigned(procinfo^.returntype.def) and
  72. (procinfo^.returntype.def<>pdef(voiddef)) then
  73. retstr:=upper(tostr(procinfo^.return_offset)+'('+att_reg2str[procinfo^.framepointer]+')')
  74. else
  75. retstr:='';
  76. c:=current_scanner^.asmgetchar;
  77. code:=new(paasmoutput,init);
  78. while not(ende) do
  79. begin
  80. { wrong placement
  81. current_scanner^.gettokenpos; }
  82. case c of
  83. 'A'..'Z','a'..'z','_' : begin
  84. current_scanner^.gettokenpos;
  85. i:=0;
  86. hs:='';
  87. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  88. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  89. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  90. or (c='_') do
  91. begin
  92. inc(i);
  93. hs[i]:=c;
  94. c:=current_scanner^.asmgetchar;
  95. end;
  96. {$ifndef TP}
  97. {$ifopt H+}
  98. setlength(hs,i);
  99. {$else}
  100. hs[0]:=chr(i);
  101. {$endif}
  102. {$else}
  103. hs[0]:=chr(i);
  104. {$endif}
  105. if upper(hs)='END' then
  106. ende:=true
  107. else
  108. begin
  109. if c=':' then
  110. begin
  111. getsym(upper(hs),false);
  112. if srsym<>nil then
  113. if (srsym^.typ = labelsym) then
  114. Begin
  115. hs:=plabelsym(srsym)^.lab^.name;
  116. plabelsym(srsym)^.lab^.is_set:=true;
  117. end
  118. else
  119. Message(asmr_w_using_defined_as_local);
  120. end
  121. else if upper(hs)='FWAIT' then
  122. FwaitWarning
  123. else
  124. { access to local variables }
  125. if assigned(aktprocsym) then
  126. begin
  127. { is the last written character an special }
  128. { char ? }
  129. if (s[length(s)]='%') and
  130. ret_in_acc(procinfo^.returntype.def) and
  131. ((pos('AX',upper(hs))>0) or
  132. (pos('AL',upper(hs))>0)) then
  133. procinfo^.funcret_state:=vs_assigned;
  134. if (s[length(s)]<>'%') and
  135. (s[length(s)]<>'$') and
  136. ((s[length(s)]<>'0') or (hs[1]<>'x')) then
  137. begin
  138. if assigned(aktprocsym^.definition^.localst) and
  139. (lexlevel >= normal_function_level) then
  140. sym:=aktprocsym^.definition^.localst^.search(upper(hs))
  141. else
  142. sym:=nil;
  143. if assigned(sym) then
  144. begin
  145. if (sym^.typ = labelsym) then
  146. Begin
  147. hs:=plabelsym(sym)^.lab^.name;
  148. end
  149. else if sym^.typ=varsym then
  150. begin
  151. {variables set are after a comma }
  152. {like in movl %eax,I }
  153. if pos(',',s) > 0 then
  154. pvarsym(sym)^.varstate:=vs_used
  155. else
  156. if (pos('MOV',upper(s)) > 0) and (pvarsym(sym)^.varstate=vs_declared) then
  157. Message1(sym_n_uninitialized_local_variable,hs);
  158. if (vo_is_external in pvarsym(sym)^.varoptions) then
  159. hs:=pvarsym(sym)^.mangledname
  160. else
  161. hs:='-'+tostr(pvarsym(sym)^.address)+
  162. '('+att_reg2str[procinfo^.framepointer]+')';
  163. end
  164. else
  165. { call to local function }
  166. if (sym^.typ=procsym) and ((pos('CALL',upper(s))>0) or
  167. (pos('LEA',upper(s))>0)) then
  168. begin
  169. hs:=pprocsym(sym)^.definition^.mangledname;
  170. end;
  171. end
  172. else
  173. begin
  174. if assigned(aktprocsym^.definition^.parast) then
  175. sym:=aktprocsym^.definition^.parast^.search(upper(hs))
  176. else
  177. sym:=nil;
  178. if assigned(sym) then
  179. begin
  180. if sym^.typ=varsym then
  181. begin
  182. l:=pvarsym(sym)^.address;
  183. { set offset }
  184. inc(l,aktprocsym^.definition^.parast^.address_fixup);
  185. hs:=tostr(l)+'('+att_reg2str[procinfo^.framepointer]+')';
  186. if pos(',',s) > 0 then
  187. pvarsym(sym)^.varstate:=vs_used;
  188. end;
  189. end
  190. { I added that but it creates a problem in line.ppi
  191. because there is a local label wbuffer and
  192. a static variable WBUFFER ...
  193. what would you decide, florian ?}
  194. else
  195. begin
  196. {$ifndef IGNOREGLOBALVAR}
  197. getsym(upper(hs),false);
  198. sym:=srsym;
  199. if assigned(sym) and (sym^.owner^.symtabletype in [unitsymtable,
  200. globalsymtable,staticsymtable]) then
  201. begin
  202. if (sym^.typ = varsym) or (sym^.typ = typedconstsym) then
  203. begin
  204. Message2(asmr_h_direct_global_to_mangled,hs,sym^.mangledname);
  205. hs:=sym^.mangledname;
  206. if sym^.typ=varsym then
  207. inc(pvarsym(sym)^.refs);
  208. end;
  209. { procs can be called or the address can be loaded }
  210. if (sym^.typ=procsym) and
  211. ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  212. begin
  213. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  214. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  215. Message2(asmr_h_direct_global_to_mangled,hs,sym^.mangledname);
  216. hs:=sym^.mangledname;
  217. end;
  218. end
  219. else
  220. {$endif TESTGLOBALVAR}
  221. if upper(hs)='__SELF' then
  222. begin
  223. if assigned(procinfo^._class) then
  224. hs:=tostr(procinfo^.selfpointer_offset)+
  225. '('+att_reg2str[procinfo^.framepointer]+')'
  226. else
  227. Message(asmr_e_cannot_use_SELF_outside_a_method);
  228. end
  229. else if upper(hs)='__RESULT' then
  230. begin
  231. if assigned(procinfo^.returntype.def) and
  232. (procinfo^.returntype.def<>pdef(voiddef)) then
  233. hs:=retstr
  234. else
  235. Message(asmr_e_void_function);
  236. end
  237. else if upper(hs)='__OLDEBP' then
  238. begin
  239. { complicate to check there }
  240. { we do it: }
  241. if lexlevel>normal_function_level then
  242. hs:=tostr(procinfo^.framepointer_offset)+
  243. '('+att_reg2str[procinfo^.framepointer]+')'
  244. else
  245. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  246. end;
  247. end;
  248. end;
  249. end;
  250. end;
  251. s:=s+hs;
  252. end;
  253. end;
  254. '{',';',#10,#13 : begin
  255. if pos(retstr,s) > 0 then
  256. procinfo^.funcret_state:=vs_assigned;
  257. writeasmline;
  258. c:=current_scanner^.asmgetchar;
  259. end;
  260. #26 : Message(scan_f_end_of_file);
  261. else
  262. begin
  263. current_scanner^.gettokenpos;
  264. {$ifndef TP}
  265. {$ifopt H+}
  266. setlength(s,length(s)+1);
  267. {$else}
  268. inc(byte(s[0]));
  269. {$endif}
  270. {$else}
  271. inc(byte(s[0]));
  272. {$endif}
  273. s[length(s)]:=c;
  274. c:=current_scanner^.asmgetchar;
  275. end;
  276. end;
  277. end;
  278. writeasmline;
  279. assemble:=genasmnode(code);
  280. end;
  281. end.
  282. {
  283. $Log$
  284. Revision 1.30 2000-02-09 13:23:02 peter
  285. * log truncated
  286. Revision 1.29 2000/01/07 01:14:36 peter
  287. * updated copyright to 2000
  288. Revision 1.28 1999/11/30 10:40:53 peter
  289. + ttype, tsymlist
  290. Revision 1.27 1999/11/17 17:05:03 pierre
  291. * Notes/hints changes
  292. Revision 1.26 1999/11/09 23:06:46 peter
  293. * esi_offset -> selfpointer_offset to be newcg compatible
  294. * hcogegen -> cgbase fixes for newcg
  295. Revision 1.25 1999/11/06 14:34:24 peter
  296. * truncated log to 20 revs
  297. Revision 1.24 1999/09/27 23:44:58 peter
  298. * procinfo is now a pointer
  299. * support for result setting in sub procedure
  300. Revision 1.23 1999/08/04 00:23:26 florian
  301. * renamed i386asm and i386base to cpuasm and cpubase
  302. Revision 1.22 1999/08/03 22:03:11 peter
  303. * moved bitmask constants to sets
  304. * some other type/const renamings
  305. }