ra386dir.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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.29 2000-01-07 01:14:36 peter
  285. * updated copyright to 2000
  286. Revision 1.28 1999/11/30 10:40:53 peter
  287. + ttype, tsymlist
  288. Revision 1.27 1999/11/17 17:05:03 pierre
  289. * Notes/hints changes
  290. Revision 1.26 1999/11/09 23:06:46 peter
  291. * esi_offset -> selfpointer_offset to be newcg compatible
  292. * hcogegen -> cgbase fixes for newcg
  293. Revision 1.25 1999/11/06 14:34:24 peter
  294. * truncated log to 20 revs
  295. Revision 1.24 1999/09/27 23:44:58 peter
  296. * procinfo is now a pointer
  297. * support for result setting in sub procedure
  298. Revision 1.23 1999/08/04 00:23:26 florian
  299. * renamed i386asm and i386base to cpuasm and cpubase
  300. Revision 1.22 1999/08/03 22:03:11 peter
  301. * moved bitmask constants to sets
  302. * some other type/const renamings
  303. Revision 1.21 1999/05/27 19:44:57 peter
  304. * removed oldasm
  305. * plabel -> pasmlabel
  306. * -a switches to source writing automaticly
  307. * assembler readers OOPed
  308. * asmsymbol automaticly external
  309. * jumptables and other label fixes for asm readers
  310. Revision 1.20 1999/05/21 13:55:15 peter
  311. * NEWLAB for label as symbol
  312. Revision 1.19 1999/05/05 22:22:02 peter
  313. * updated messages
  314. Revision 1.18 1999/05/01 13:24:40 peter
  315. * merged nasm compiler
  316. * old asm moved to oldasm/
  317. Revision 1.17 1999/03/31 13:55:18 peter
  318. * assembler inlining working for ag386bin
  319. Revision 1.16 1999/03/24 23:17:22 peter
  320. * fixed bugs 212,222,225,227,229,231,233
  321. Revision 1.15 1999/03/01 13:22:26 pierre
  322. * varsym refs incremented
  323. Revision 1.14 1999/02/22 02:15:36 peter
  324. * updates for ag386bin
  325. Revision 1.13 1999/01/27 13:04:12 pierre
  326. * bug with static vars in assembler readers
  327. Revision 1.12 1999/01/10 15:37:57 peter
  328. * moved some tables from ra386*.pas -> i386.pas
  329. + start of coff writer
  330. * renamed asmutils unit to rautils
  331. Revision 1.11 1998/11/17 00:26:12 peter
  332. * fixed for $H+
  333. Revision 1.10 1998/11/13 15:40:28 pierre
  334. + added -Se in Makefile cvstest target
  335. + lexlevel cleanup
  336. normal_function_level main_program_level and unit_init_level defined
  337. * tins_cache grown to A_EMMS (gave range check error in asm readers)
  338. (test added in code !)
  339. * -Un option was wrong
  340. * _FAIL and _SELF only keyword inside
  341. constructors and methods respectively
  342. Revision 1.9 1998/10/20 08:06:57 pierre
  343. * several memory corruptions due to double freemem solved
  344. => never use p^.loc.location:=p^.left^.loc.location;
  345. + finally I added now by default
  346. that ra386dir translates global and unit symbols
  347. + added a first field in tsymtable and
  348. a nextsym field in tsym
  349. (this allows to obtain ordered type info for
  350. records and objects in gdb !)
  351. Revision 1.8 1998/09/04 08:42:08 peter
  352. * updated some error messages
  353. Revision 1.7 1998/09/03 17:39:05 florian
  354. + better code for type conversation longint/dword to real type
  355. Revision 1.6 1998/09/03 17:08:47 pierre
  356. * better lines for stabs
  357. (no scroll back to if before else part
  358. no return to case line at jump outside case)
  359. + source lines also if not in order
  360. Revision 1.5 1998/08/21 08:45:51 pierre
  361. * better line info for asm statements
  362. }