ra386dir.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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,hcodegen,globals,scanner,aasm,cpubase,cpuasm,
  26. cobjects,symconst,symtable,types,verbose,
  27. rautils,ra386;
  28. function assemble : ptree;
  29. var
  30. retstr,s,hs : string;
  31. c : char;
  32. ende : boolean;
  33. sym : psym;
  34. code : paasmoutput;
  35. i,l : longint;
  36. procedure writeasmline;
  37. var
  38. i : longint;
  39. begin
  40. i:=length(s);
  41. while (i>0) and (s[i] in [' ',#9]) do
  42. dec(i);
  43. {$ifndef TP}
  44. {$ifopt H+}
  45. setlength(s,i);
  46. {$else}
  47. s[0]:=chr(i);
  48. {$endif}
  49. {$else}
  50. s[0]:=chr(i);
  51. {$endif}
  52. if s<>'' then
  53. code^.concat(new(pai_direct,init(strpnew(s))));
  54. { consider it set function set if the offset was loaded }
  55. if assigned(procinfo^.retdef) and
  56. (pos(retstr,upper(s))>0) then
  57. procinfo^.funcret_is_valid:=true;
  58. s:='';
  59. end;
  60. begin
  61. ende:=false;
  62. s:='';
  63. if assigned(procinfo^.retdef) and
  64. is_fpu(procinfo^.retdef) then
  65. procinfo^.funcret_is_valid:=true;
  66. if assigned(procinfo^.retdef) and
  67. (procinfo^.retdef<>pdef(voiddef)) then
  68. retstr:=upper(tostr(procinfo^.retoffset)+'('+att_reg2str[procinfo^.framepointer]+')')
  69. else
  70. retstr:='';
  71. c:=current_scanner^.asmgetchar;
  72. code:=new(paasmoutput,init);
  73. while not(ende) do
  74. begin
  75. { wrong placement
  76. current_scanner^.gettokenpos; }
  77. case c of
  78. 'A'..'Z','a'..'z','_' : begin
  79. current_scanner^.gettokenpos;
  80. i:=0;
  81. hs:='';
  82. while ((ord(c)>=ord('A')) and (ord(c)<=ord('Z')))
  83. or ((ord(c)>=ord('a')) and (ord(c)<=ord('z')))
  84. or ((ord(c)>=ord('0')) and (ord(c)<=ord('9')))
  85. or (c='_') do
  86. begin
  87. inc(i);
  88. hs[i]:=c;
  89. c:=current_scanner^.asmgetchar;
  90. end;
  91. {$ifndef TP}
  92. {$ifopt H+}
  93. setlength(hs,i);
  94. {$else}
  95. hs[0]:=chr(i);
  96. {$endif}
  97. {$else}
  98. hs[0]:=chr(i);
  99. {$endif}
  100. if upper(hs)='END' then
  101. ende:=true
  102. else
  103. begin
  104. if c=':' then
  105. begin
  106. getsym(upper(hs),false);
  107. if srsym<>nil then
  108. if (srsym^.typ = labelsym) then
  109. Begin
  110. hs:=plabelsym(srsym)^.lab^.name;
  111. plabelsym(srsym)^.lab^.is_set:=true;
  112. end
  113. else
  114. Message(asmr_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:=plabelsym(sym)^.lab^.name;
  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)^.varstate:=vs_used
  150. else
  151. if (pos('MOV',upper(s)) > 0) and (pvarsym(sym)^.varstate=vs_declared) then
  152. Message1(sym_n_uninitialized_local_variable,hs);
  153. if (vo_is_external in pvarsym(sym)^.varoptions) then
  154. hs:=pvarsym(sym)^.mangledname
  155. else
  156. hs:='-'+tostr(pvarsym(sym)^.address)+'('+att_reg2str[procinfo^.framepointer]+')';
  157. end
  158. else
  159. { call to local function }
  160. if (sym^.typ=procsym) and ((pos('CALL',upper(s))>0) or
  161. (pos('LEA',upper(s))>0)) then
  162. begin
  163. hs:=pprocsym(sym)^.definition^.mangledname;
  164. end;
  165. end
  166. else
  167. begin
  168. if assigned(aktprocsym^.definition^.parast) then
  169. sym:=aktprocsym^.definition^.parast^.search(upper(hs))
  170. else
  171. sym:=nil;
  172. if assigned(sym) then
  173. begin
  174. if sym^.typ=varsym then
  175. begin
  176. l:=pvarsym(sym)^.address;
  177. { set offset }
  178. inc(l,aktprocsym^.definition^.parast^.address_fixup);
  179. hs:=tostr(l)+'('+att_reg2str[procinfo^.framepointer]+')';
  180. if pos(',',s) > 0 then
  181. pvarsym(sym)^.varstate:=vs_used;
  182. end;
  183. end
  184. { I added that but it creates a problem in line.ppi
  185. because there is a local label wbuffer and
  186. a static variable WBUFFER ...
  187. what would you decide, florian ?}
  188. else
  189. begin
  190. {$ifndef IGNOREGLOBALVAR}
  191. getsym(upper(hs),false);
  192. sym:=srsym;
  193. if assigned(sym) and (sym^.owner^.symtabletype in [unitsymtable,
  194. globalsymtable,staticsymtable]) then
  195. begin
  196. if (sym^.typ = varsym) or (sym^.typ = typedconstsym) then
  197. begin
  198. Message2(asmr_h_direct_global_to_mangled,hs,sym^.mangledname);
  199. hs:=sym^.mangledname;
  200. if sym^.typ=varsym then
  201. inc(pvarsym(sym)^.refs);
  202. end;
  203. { procs can be called or the address can be loaded }
  204. if (sym^.typ=procsym) and
  205. ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  206. begin
  207. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  208. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  209. Message2(asmr_h_direct_global_to_mangled,hs,sym^.mangledname);
  210. hs:=sym^.mangledname;
  211. end;
  212. end
  213. else
  214. {$endif TESTGLOBALVAR}
  215. if upper(hs)='__SELF' then
  216. begin
  217. if assigned(procinfo^._class) then
  218. hs:=tostr(procinfo^.ESI_offset)+'('+att_reg2str[procinfo^.framepointer]+')'
  219. else
  220. Message(asmr_e_cannot_use_SELF_outside_a_method);
  221. end
  222. else if upper(hs)='__RESULT' then
  223. begin
  224. if assigned(procinfo^.retdef) and
  225. (procinfo^.retdef<>pdef(voiddef)) then
  226. hs:=retstr
  227. else
  228. Message(asmr_e_void_function);
  229. end
  230. else if upper(hs)='__OLDEBP' then
  231. begin
  232. { complicate to check there }
  233. { we do it: }
  234. if lexlevel>normal_function_level then
  235. hs:=tostr(procinfo^.framepointer_offset)+
  236. '('+att_reg2str[procinfo^.framepointer]+')'
  237. else
  238. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  239. end;
  240. end;
  241. end;
  242. end;
  243. end;
  244. s:=s+hs;
  245. end;
  246. end;
  247. '{',';',#10,#13 : begin
  248. if pos(retstr,s) > 0 then
  249. procinfo^.funcret_is_valid:=true;
  250. writeasmline;
  251. c:=current_scanner^.asmgetchar;
  252. end;
  253. #26 : Message(scan_f_end_of_file);
  254. else
  255. begin
  256. current_scanner^.gettokenpos;
  257. {$ifndef TP}
  258. {$ifopt H+}
  259. setlength(s,length(s)+1);
  260. {$else}
  261. inc(byte(s[0]));
  262. {$endif}
  263. {$else}
  264. inc(byte(s[0]));
  265. {$endif}
  266. s[length(s)]:=c;
  267. c:=current_scanner^.asmgetchar;
  268. end;
  269. end;
  270. end;
  271. writeasmline;
  272. assemble:=genasmnode(code);
  273. end;
  274. end.
  275. {
  276. $Log$
  277. Revision 1.25 1999-11-06 14:34:24 peter
  278. * truncated log to 20 revs
  279. Revision 1.24 1999/09/27 23:44:58 peter
  280. * procinfo is now a pointer
  281. * support for result setting in sub procedure
  282. Revision 1.23 1999/08/04 00:23:26 florian
  283. * renamed i386asm and i386base to cpuasm and cpubase
  284. Revision 1.22 1999/08/03 22:03:11 peter
  285. * moved bitmask constants to sets
  286. * some other type/const renamings
  287. Revision 1.21 1999/05/27 19:44:57 peter
  288. * removed oldasm
  289. * plabel -> pasmlabel
  290. * -a switches to source writing automaticly
  291. * assembler readers OOPed
  292. * asmsymbol automaticly external
  293. * jumptables and other label fixes for asm readers
  294. Revision 1.20 1999/05/21 13:55:15 peter
  295. * NEWLAB for label as symbol
  296. Revision 1.19 1999/05/05 22:22:02 peter
  297. * updated messages
  298. Revision 1.18 1999/05/01 13:24:40 peter
  299. * merged nasm compiler
  300. * old asm moved to oldasm/
  301. Revision 1.17 1999/03/31 13:55:18 peter
  302. * assembler inlining working for ag386bin
  303. Revision 1.16 1999/03/24 23:17:22 peter
  304. * fixed bugs 212,222,225,227,229,231,233
  305. Revision 1.15 1999/03/01 13:22:26 pierre
  306. * varsym refs incremented
  307. Revision 1.14 1999/02/22 02:15:36 peter
  308. * updates for ag386bin
  309. Revision 1.13 1999/01/27 13:04:12 pierre
  310. * bug with static vars in assembler readers
  311. Revision 1.12 1999/01/10 15:37:57 peter
  312. * moved some tables from ra386*.pas -> i386.pas
  313. + start of coff writer
  314. * renamed asmutils unit to rautils
  315. Revision 1.11 1998/11/17 00:26:12 peter
  316. * fixed for $H+
  317. Revision 1.10 1998/11/13 15:40:28 pierre
  318. + added -Se in Makefile cvstest target
  319. + lexlevel cleanup
  320. normal_function_level main_program_level and unit_init_level defined
  321. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  322. (test added in code !)
  323. * -Un option was wrong
  324. * _FAIL and _SELF only keyword inside
  325. constructors and methods respectively
  326. Revision 1.9 1998/10/20 08:06:57 pierre
  327. * several memory corruptions due to double freemem solved
  328. => never use p^.loc.location:=p^.left^.loc.location;
  329. + finally I added now by default
  330. that ra386dir translates global and unit symbols
  331. + added a first field in tsymtable and
  332. a nextsym field in tsym
  333. (this allows to obtain ordered type info for
  334. records and objects in gdb !)
  335. Revision 1.8 1998/09/04 08:42:08 peter
  336. * updated some error messages
  337. Revision 1.7 1998/09/03 17:39:05 florian
  338. + better code for type conversation longint/dword to real type
  339. Revision 1.6 1998/09/03 17:08:47 pierre
  340. * better lines for stabs
  341. (no scroll back to if before else part
  342. no return to case line at jump outside case)
  343. + source lines also if not in order
  344. Revision 1.5 1998/08/21 08:45:51 pierre
  345. * better line info for asm statements
  346. }