ra386dir.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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,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^.retdef) 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^.retdef) and
  69. is_fpu(procinfo^.retdef) then
  70. procinfo^.funcret_state:=vs_assigned;
  71. if assigned(procinfo^.retdef) and
  72. (procinfo^.retdef<>pdef(voiddef)) then
  73. retstr:=upper(tostr(procinfo^.retoffset)+'('+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^.retdef) 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)+'('+att_reg2str[procinfo^.framepointer]+')';
  162. end
  163. else
  164. { call to local function }
  165. if (sym^.typ=procsym) and ((pos('CALL',upper(s))>0) or
  166. (pos('LEA',upper(s))>0)) then
  167. begin
  168. hs:=pprocsym(sym)^.definition^.mangledname;
  169. end;
  170. end
  171. else
  172. begin
  173. if assigned(aktprocsym^.definition^.parast) then
  174. sym:=aktprocsym^.definition^.parast^.search(upper(hs))
  175. else
  176. sym:=nil;
  177. if assigned(sym) then
  178. begin
  179. if sym^.typ=varsym then
  180. begin
  181. l:=pvarsym(sym)^.address;
  182. { set offset }
  183. inc(l,aktprocsym^.definition^.parast^.address_fixup);
  184. hs:=tostr(l)+'('+att_reg2str[procinfo^.framepointer]+')';
  185. if pos(',',s) > 0 then
  186. pvarsym(sym)^.varstate:=vs_used;
  187. end;
  188. end
  189. { I added that but it creates a problem in line.ppi
  190. because there is a local label wbuffer and
  191. a static variable WBUFFER ...
  192. what would you decide, florian ?}
  193. else
  194. begin
  195. {$ifndef IGNOREGLOBALVAR}
  196. getsym(upper(hs),false);
  197. sym:=srsym;
  198. if assigned(sym) and (sym^.owner^.symtabletype in [unitsymtable,
  199. globalsymtable,staticsymtable]) then
  200. begin
  201. if (sym^.typ = varsym) or (sym^.typ = typedconstsym) then
  202. begin
  203. Message2(asmr_h_direct_global_to_mangled,hs,sym^.mangledname);
  204. hs:=sym^.mangledname;
  205. if sym^.typ=varsym then
  206. inc(pvarsym(sym)^.refs);
  207. end;
  208. { procs can be called or the address can be loaded }
  209. if (sym^.typ=procsym) and
  210. ((pos('CALL',upper(s))>0) or (pos('LEA',upper(s))>0)) then
  211. begin
  212. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  213. Message1(asmr_w_direct_global_is_overloaded_func,hs);
  214. Message2(asmr_h_direct_global_to_mangled,hs,sym^.mangledname);
  215. hs:=sym^.mangledname;
  216. end;
  217. end
  218. else
  219. {$endif TESTGLOBALVAR}
  220. if upper(hs)='__SELF' then
  221. begin
  222. if assigned(procinfo^._class) then
  223. hs:=tostr(procinfo^.selfpointer_offset)+'('+att_reg2str[procinfo^.framepointer]+')'
  224. else
  225. Message(asmr_e_cannot_use_SELF_outside_a_method);
  226. end
  227. else if upper(hs)='__RESULT' then
  228. begin
  229. if assigned(procinfo^.retdef) and
  230. (procinfo^.retdef<>pdef(voiddef)) then
  231. hs:=retstr
  232. else
  233. Message(asmr_e_void_function);
  234. end
  235. else if upper(hs)='__OLDEBP' then
  236. begin
  237. { complicate to check there }
  238. { we do it: }
  239. if lexlevel>normal_function_level then
  240. hs:=tostr(procinfo^.framepointer_offset)+
  241. '('+att_reg2str[procinfo^.framepointer]+')'
  242. else
  243. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  244. end;
  245. end;
  246. end;
  247. end;
  248. end;
  249. s:=s+hs;
  250. end;
  251. end;
  252. '{',';',#10,#13 : begin
  253. if pos(retstr,s) > 0 then
  254. procinfo^.funcret_state:=vs_assigned;
  255. writeasmline;
  256. c:=current_scanner^.asmgetchar;
  257. end;
  258. #26 : Message(scan_f_end_of_file);
  259. else
  260. begin
  261. current_scanner^.gettokenpos;
  262. {$ifndef TP}
  263. {$ifopt H+}
  264. setlength(s,length(s)+1);
  265. {$else}
  266. inc(byte(s[0]));
  267. {$endif}
  268. {$else}
  269. inc(byte(s[0]));
  270. {$endif}
  271. s[length(s)]:=c;
  272. c:=current_scanner^.asmgetchar;
  273. end;
  274. end;
  275. end;
  276. writeasmline;
  277. assemble:=genasmnode(code);
  278. end;
  279. end.
  280. {
  281. $Log$
  282. Revision 1.27 1999-11-17 17:05:03 pierre
  283. * Notes/hints changes
  284. Revision 1.26 1999/11/09 23:06:46 peter
  285. * esi_offset -> selfpointer_offset to be newcg compatible
  286. * hcogegen -> cgbase fixes for newcg
  287. Revision 1.25 1999/11/06 14:34:24 peter
  288. * truncated log to 20 revs
  289. Revision 1.24 1999/09/27 23:44:58 peter
  290. * procinfo is now a pointer
  291. * support for result setting in sub procedure
  292. Revision 1.23 1999/08/04 00:23:26 florian
  293. * renamed i386asm and i386base to cpuasm and cpubase
  294. Revision 1.22 1999/08/03 22:03:11 peter
  295. * moved bitmask constants to sets
  296. * some other type/const renamings
  297. Revision 1.21 1999/05/27 19:44:57 peter
  298. * removed oldasm
  299. * plabel -> pasmlabel
  300. * -a switches to source writing automaticly
  301. * assembler readers OOPed
  302. * asmsymbol automaticly external
  303. * jumptables and other label fixes for asm readers
  304. Revision 1.20 1999/05/21 13:55:15 peter
  305. * NEWLAB for label as symbol
  306. Revision 1.19 1999/05/05 22:22:02 peter
  307. * updated messages
  308. Revision 1.18 1999/05/01 13:24:40 peter
  309. * merged nasm compiler
  310. * old asm moved to oldasm/
  311. Revision 1.17 1999/03/31 13:55:18 peter
  312. * assembler inlining working for ag386bin
  313. Revision 1.16 1999/03/24 23:17:22 peter
  314. * fixed bugs 212,222,225,227,229,231,233
  315. Revision 1.15 1999/03/01 13:22:26 pierre
  316. * varsym refs incremented
  317. Revision 1.14 1999/02/22 02:15:36 peter
  318. * updates for ag386bin
  319. Revision 1.13 1999/01/27 13:04:12 pierre
  320. * bug with static vars in assembler readers
  321. Revision 1.12 1999/01/10 15:37:57 peter
  322. * moved some tables from ra386*.pas -> i386.pas
  323. + start of coff writer
  324. * renamed asmutils unit to rautils
  325. Revision 1.11 1998/11/17 00:26:12 peter
  326. * fixed for $H+
  327. Revision 1.10 1998/11/13 15:40:28 pierre
  328. + added -Se in Makefile cvstest target
  329. + lexlevel cleanup
  330. normal_function_level main_program_level and unit_init_level defined
  331. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  332. (test added in code !)
  333. * -Un option was wrong
  334. * _FAIL and _SELF only keyword inside
  335. constructors and methods respectively
  336. Revision 1.9 1998/10/20 08:06:57 pierre
  337. * several memory corruptions due to double freemem solved
  338. => never use p^.loc.location:=p^.left^.loc.location;
  339. + finally I added now by default
  340. that ra386dir translates global and unit symbols
  341. + added a first field in tsymtable and
  342. a nextsym field in tsym
  343. (this allows to obtain ordered type info for
  344. records and objects in gdb !)
  345. Revision 1.8 1998/09/04 08:42:08 peter
  346. * updated some error messages
  347. Revision 1.7 1998/09/03 17:39:05 florian
  348. + better code for type conversation longint/dword to real type
  349. Revision 1.6 1998/09/03 17:08:47 pierre
  350. * better lines for stabs
  351. (no scroll back to if before else part
  352. no return to case line at jump outside case)
  353. + source lines also if not in order
  354. Revision 1.5 1998/08/21 08:45:51 pierre
  355. * better line info for asm statements
  356. }