ag386nsm.pas 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. {
  2. $Id$
  3. Copyright (c) 1996,97 by Florian Klaempfl
  4. This unit implements an asmoutput class for the Nasm assembler with
  5. Intel syntax for the i386+
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************
  18. }
  19. { R- Necessary for the in [] }
  20. {$ifdef TP}
  21. {$N+,E+,R-}
  22. {$endif}
  23. unit ag386nsm;
  24. interface
  25. uses aasm,assemble;
  26. type
  27. pi386nasmasmlist=^ti386nasmasmlist;
  28. ti386nasmasmlist = object(tasmlist)
  29. procedure WriteTree(p:paasmoutput);virtual;
  30. procedure WriteAsmList;virtual;
  31. end;
  32. implementation
  33. uses
  34. dos,globals,systems,cobjects,i386,
  35. strings,files,verbose
  36. {$ifdef GDB}
  37. ,gdb
  38. {$endif GDB}
  39. ;
  40. const
  41. line_length = 70;
  42. extstr : array[EXT_NEAR..EXT_ABS] of String[8] =
  43. ('NEAR','FAR','PROC','BYTE','WORD','DWORD',
  44. 'CODEPTR','DATAPTR','FWORD','PWORD','QWORD','TBYTE','ABS');
  45. function double2str(d : double) : string;
  46. var
  47. hs : string;
  48. p : byte;
  49. begin
  50. str(d,hs);
  51. { nasm expects a lowercase e }
  52. p:=pos('E',hs);
  53. if p>0 then
  54. hs[p]:='e';
  55. p:=pos('+',hs);
  56. if p>0 then
  57. delete(hs,p,1);
  58. double2str:=lower(hs);
  59. end;
  60. function extended2str(e : extended) : string;
  61. var
  62. hs : string;
  63. p : byte;
  64. begin
  65. {$ifdef VER0_99_5}
  66. str(double(e),hs);
  67. {$else}
  68. str(e,hs);
  69. {$endif}
  70. { nasm expects a lowercase e }
  71. p:=pos('E',hs);
  72. if p>0 then
  73. hs[p]:='e';
  74. p:=pos('+',hs);
  75. if p>0 then
  76. delete(hs,p,1);
  77. extended2str:=lower(hs);
  78. end;
  79. function comp2str(d : bestreal) : string;
  80. type
  81. pdouble = ^double;
  82. var
  83. c : comp;
  84. dd : pdouble;
  85. begin
  86. {$ifdef TP}
  87. c:=d;
  88. {$else}
  89. c:=comp(d);
  90. {$endif}
  91. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  92. comp2str:=double2str(dd^);
  93. end;
  94. function getreferencestring(const ref : treference) : string;
  95. var
  96. s : string;
  97. first : boolean;
  98. begin
  99. if ref.isintvalue then
  100. s:= tostr(ref.offset)
  101. else
  102. with ref do
  103. begin
  104. first:=true;
  105. if ref.segment<>R_DEFAULT_SEG then
  106. s:='['+int_reg2str[segment]+':'
  107. else
  108. s:='[';
  109. if assigned(symbol) then
  110. begin
  111. s:=s+symbol^;
  112. first:=false;
  113. end;
  114. if (base<>R_NO) then
  115. begin
  116. if not(first) then
  117. s:=s+'+'
  118. else
  119. first:=false;
  120. s:=s+int_reg2str[base];
  121. end;
  122. if (index<>R_NO) then
  123. begin
  124. if not(first) then
  125. s:=s+'+'
  126. else
  127. first:=false;
  128. s:=s+int_reg2str[index];
  129. if scalefactor<>0 then
  130. s:=s+'*'+tostr(scalefactor);
  131. end;
  132. if offset<0 then
  133. s:=s+tostr(offset)
  134. else if (offset>0) then
  135. s:=s+'+'+tostr(offset);
  136. s:=s+']';
  137. end;
  138. getreferencestring:=s;
  139. end;
  140. function getopstr(t : byte;o : pointer;s : topsize; _operator: tasmop;dest : boolean) : string;
  141. var
  142. hs : string;
  143. begin
  144. case t of
  145. top_reg : getopstr:=int_nasmreg2str[tregister(o)];
  146. top_const,
  147. top_ref : begin
  148. if t=top_const then
  149. hs := tostr(longint(o))
  150. else
  151. hs:=getreferencestring(preference(o)^);
  152. if not ((_operator = A_LEA) or (_operator = A_LGS) or
  153. (_operator = A_LSS) or (_operator = A_LFS) or
  154. (_operator = A_LES) or (_operator = A_LDS) or
  155. (_operator = A_SHR) or (_operator = A_SHL) or
  156. (_operator = A_SAR) or (_operator = A_SAL) or
  157. (_operator = A_OUT) or (_operator = A_IN)) then
  158. begin
  159. case s of
  160. S_B : hs:='byte '+hs;
  161. S_W : hs:='word '+hs;
  162. S_L : hs:='dword '+hs;
  163. S_IS : hs:='word '+hs;
  164. S_IL : hs:='dword '+hs;
  165. S_IQ : hs:='qword '+hs;
  166. S_FS : hs:='dword '+hs;
  167. S_FL : hs:='qword '+hs;
  168. S_FX : hs:='tword '+hs;
  169. S_BW : if dest then
  170. hs:='word '+hs
  171. else
  172. hs:='byte '+hs;
  173. S_BL : if dest then
  174. hs:='dword '+hs
  175. else
  176. hs:='byte '+hs;
  177. S_WL : if dest then
  178. hs:='dword '+hs
  179. else
  180. hs:='word '+hs;
  181. end
  182. end;
  183. getopstr:=hs;
  184. end;
  185. top_symbol : begin
  186. hs[0]:=chr(strlen(pchar(pcsymbol(o)^.symbol)));
  187. move(pchar(pcsymbol(o)^.symbol)^,hs[1],byte(hs[0]));
  188. hs:='dword '+hs;
  189. if pcsymbol(o)^.offset>0 then
  190. hs:=hs+'+'+tostr(pcsymbol(o)^.offset)
  191. else
  192. if pcsymbol(o)^.offset<0 then
  193. hs:=hs+tostr(pcsymbol(o)^.offset);
  194. getopstr:=hs;
  195. end;
  196. else
  197. internalerror(10001);
  198. end;
  199. end;
  200. function getopstr_jmp(t : byte;o : pointer) : string;
  201. var
  202. hs : string;
  203. begin
  204. case t of
  205. top_reg : getopstr_jmp:=int_reg2str[tregister(o)];
  206. top_ref : getopstr_jmp:=getreferencestring(preference(o)^);
  207. top_const : getopstr_jmp:=tostr(longint(o));
  208. top_symbol : begin
  209. hs[0]:=chr(strlen(pchar(pcsymbol(o)^.symbol)));
  210. move(pchar(pcsymbol(o)^.symbol)^,hs[1],byte(hs[0]));
  211. if pcsymbol(o)^.offset>0 then
  212. hs:=hs+'+'+tostr(pcsymbol(o)^.offset)
  213. else
  214. if pcsymbol(o)^.offset<0 then
  215. hs:=hs+tostr(pcsymbol(o)^.offset);
  216. getopstr_jmp:=hs;
  217. end;
  218. else
  219. internalerror(10001);
  220. end;
  221. end;
  222. {****************************************************************************
  223. Ti386nasmasmlist
  224. ****************************************************************************}
  225. var
  226. LastSec : tsection;
  227. const
  228. ait_const2str:array[ait_const_32bit..ait_const_8bit] of string[8]=
  229. (#9'DD'#9,#9'DW'#9,#9'DB'#9);
  230. ait_section2nasmstr : array[tsection] of string[6]=
  231. ('','.text','.data','.bss','.idata');
  232. Function PadTabs(p:pchar;addch:char):string;
  233. var
  234. s : string;
  235. i : longint;
  236. begin
  237. i:=strlen(p);
  238. if addch<>#0 then
  239. begin
  240. inc(i);
  241. s:=StrPas(p)+addch;
  242. end
  243. else
  244. s:=StrPas(p);
  245. if i<8 then
  246. PadTabs:=s+#9#9
  247. else
  248. PadTabs:=s+#9;
  249. end;
  250. procedure ti386nasmasmlist.WriteTree(p:paasmoutput);
  251. type
  252. twowords=record
  253. word1,word2:word;
  254. end;
  255. var
  256. s,
  257. prefix,
  258. suffix : string;
  259. hp : pai;
  260. counter,
  261. lines,
  262. i,j,l : longint;
  263. consttyp : tait;
  264. found,
  265. quoted : boolean;
  266. begin
  267. if not assigned(p) then
  268. exit;
  269. hp:=pai(p^.first);
  270. while assigned(hp) do
  271. begin
  272. case hp^.typ of
  273. ait_comment : Begin
  274. AsmWrite(target_asm.comment);
  275. AsmWritePChar(pai_asm_comment(hp)^.str);
  276. AsmLn;
  277. End;
  278. ait_regalloc,
  279. ait_regdealloc :;
  280. ait_section : begin
  281. if pai_section(hp)^.sec<>sec_none then
  282. begin
  283. AsmLn;
  284. AsmWriteLn('SECTION '+ait_section2nasmstr[pai_section(hp)^.sec]);
  285. end;
  286. LastSec:=pai_section(hp)^.sec;
  287. end;
  288. ait_align : AsmWriteLn(#9'ALIGN '+tostr(pai_align(hp)^.aligntype));
  289. ait_external : AsmWriteLn('EXTERN '+StrPas(pai_external(hp)^.name));
  290. ait_datablock : begin
  291. if pai_datablock(hp)^.is_global then
  292. AsmWriteLn(#9'GLOBAL '+StrPas(pai_datablock(hp)^.name));
  293. AsmWriteLn(PadTabs(pai_datablock(hp)^.name,':')+'RESB'#9+tostr(pai_datablock(hp)^.size));
  294. end;
  295. ait_const_32bit,
  296. ait_const_8bit,
  297. ait_const_16bit : begin
  298. AsmWrite(ait_const2str[hp^.typ]+tostr(pai_const(hp)^.value));
  299. consttyp:=hp^.typ;
  300. l:=0;
  301. repeat
  302. found:=(not (Pai(hp^.next)=nil)) and (Pai(hp^.next)^.typ=consttyp);
  303. if found then
  304. begin
  305. hp:=Pai(hp^.next);
  306. s:=','+tostr(pai_const(hp)^.value);
  307. AsmWrite(s);
  308. inc(l,length(s));
  309. end;
  310. until (not found) or (l>line_length);
  311. AsmLn;
  312. end;
  313. ait_const_symbol : begin
  314. AsmWrite(#9#9+'DD '#9);
  315. AsmWritePChar(pchar(pai_const(hp)^.value));
  316. AsmLn;
  317. end;
  318. ait_const_symbol_offset : begin
  319. AsmWrite(#9#9+'DD '#9);
  320. AsmWritePChar(pai_const_symbol_offset(hp)^.name);
  321. if pai_const_symbol_offset(hp)^.offset>0 then
  322. AsmWrite('+'+tostr(pai_const_symbol_offset(hp)^.offset))
  323. else if pai_const_symbol_offset(hp)^.offset<0 then
  324. AsmWrite(tostr(pai_const_symbol_offset(hp)^.offset));
  325. AsmLn;
  326. end;
  327. ait_real_32bit : AsmWriteLn(#9#9'DD'#9+double2str(pai_single(hp)^.value));
  328. ait_real_64bit : AsmWriteLn(#9#9'DQ'#9+double2str(pai_double(hp)^.value));
  329. ait_real_extended : AsmWriteLn(#9#9'DT'#9+extended2str(pai_extended(hp)^.value));
  330. ait_comp : AsmWriteLn(#9#9'DQ'#9+comp2str(pai_extended(hp)^.value));
  331. ait_string : begin
  332. counter := 0;
  333. lines := pai_string(hp)^.len div line_length;
  334. { separate lines in different parts }
  335. if pai_string(hp)^.len > 0 then
  336. Begin
  337. for j := 0 to lines-1 do
  338. begin
  339. AsmWrite(#9#9'DB'#9);
  340. quoted:=false;
  341. for i:=counter to counter+line_length do
  342. begin
  343. { it is an ascii character. }
  344. if (ord(pai_string(hp)^.str[i])>31) and
  345. (ord(pai_string(hp)^.str[i])<128) and
  346. (pai_string(hp)^.str[i]<>'"') then
  347. begin
  348. if not(quoted) then
  349. begin
  350. if i>counter then
  351. AsmWrite(',');
  352. AsmWrite('"');
  353. end;
  354. AsmWrite(pai_string(hp)^.str[i]);
  355. quoted:=true;
  356. end { if > 31 and < 128 and ord('"') }
  357. else
  358. begin
  359. if quoted then
  360. AsmWrite('"');
  361. if i>counter then
  362. AsmWrite(',');
  363. quoted:=false;
  364. AsmWrite(tostr(ord(pai_string(hp)^.str[i])));
  365. end;
  366. end; { end for i:=0 to... }
  367. if quoted then AsmWrite('"');
  368. AsmWrite(target_os.newline);
  369. counter := counter+line_length;
  370. end; { end for j:=0 ... }
  371. { do last line of lines }
  372. AsmWrite(#9#9'DB'#9);
  373. quoted:=false;
  374. for i:=counter to pai_string(hp)^.len-1 do
  375. begin
  376. { it is an ascii character. }
  377. if (ord(pai_string(hp)^.str[i])>31) and
  378. (ord(pai_string(hp)^.str[i])<128) and
  379. (pai_string(hp)^.str[i]<>'"') then
  380. begin
  381. if not(quoted) then
  382. begin
  383. if i>counter then
  384. AsmWrite(',');
  385. AsmWrite('"');
  386. end;
  387. AsmWrite(pai_string(hp)^.str[i]);
  388. quoted:=true;
  389. end { if > 31 and < 128 and " }
  390. else
  391. begin
  392. if quoted then
  393. AsmWrite('"');
  394. if i>counter then
  395. AsmWrite(',');
  396. quoted:=false;
  397. AsmWrite(tostr(ord(pai_string(hp)^.str[i])));
  398. end;
  399. end; { end for i:=0 to... }
  400. if quoted then
  401. AsmWrite('"');
  402. end;
  403. AsmLn;
  404. end;
  405. ait_label : begin
  406. if pai_label(hp)^.l^.is_used then
  407. AsmWriteLn(lab2str(pai_label(hp)^.l)+':');
  408. end;
  409. ait_direct : begin
  410. AsmWritePChar(pai_direct(hp)^.str);
  411. AsmLn;
  412. end;
  413. ait_labeled_instruction :
  414. begin
  415. if not (pai_labeled(hp)^._operator in [A_JMP,A_LOOP,A_LOOPZ,A_LOOPE,
  416. A_LOOPNZ,A_LOOPNE,A_JCXZ,A_JECXZ]) then
  417. AsmWriteLn(#9#9+int_op2str[pai_labeled(hp)^._operator]+#9+'near '+lab2str(pai_labeled(hp)^.lab))
  418. else
  419. AsmWriteLn(#9#9+int_op2str[pai_labeled(hp)^._operator]+#9+lab2str(pai_labeled(hp)^.lab));
  420. end;
  421. ait_symbol : begin
  422. if pai_symbol(hp)^.is_global then
  423. AsmWriteLn(#9'GLOBAL '+StrPas(pai_symbol(hp)^.name));
  424. AsmWritePChar(pai_symbol(hp)^.name);
  425. if assigned(hp^.next) and not(pai(hp^.next)^.typ in
  426. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  427. ait_const_symbol,ait_const_symbol_offset,
  428. ait_real_64bit,ait_string]) then
  429. AsmWriteLn(':')
  430. end;
  431. ait_instruction : begin
  432. suffix:='';
  433. prefix:= '';
  434. { added prefix instructions, must be on same line as opcode }
  435. if (pai386(hp)^.op1t = top_none) and
  436. ((pai386(hp)^._operator = A_REP) or
  437. (pai386(hp)^._operator = A_LOCK) or
  438. (pai386(hp)^._operator = A_REPE) or
  439. (pai386(hp)^._operator = A_REPNE)) then
  440. Begin
  441. prefix:=int_op2str[pai386(hp)^._operator]+#9;
  442. hp:=Pai(hp^.next);
  443. { this is theorically impossible... }
  444. if hp=nil then
  445. begin
  446. s:=#9#9+prefix;
  447. AsmWriteLn(s);
  448. break;
  449. end;
  450. { nasm prefers prefix on a line alone }
  451. AsmWriteln(#9#9+prefix);
  452. prefix:='';
  453. end
  454. else
  455. prefix:= '';
  456. { A_FNSTS need the w as suffix at least for nasm}
  457. if (pai386(hp)^._operator = A_FNSTS) then
  458. pai386(hp)^._operator:=A_FNSTSW
  459. else
  460. if (pai386(hp)^._operator = A_FSTS) then
  461. pai386(hp)^._operator:=A_FSTSW;
  462. if pai386(hp)^.op1t<>top_none then
  463. begin
  464. if pai386(hp)^._operator in [A_CALL] then
  465. s:=getopstr_jmp(pai386(hp)^.op1t,pai386(hp)^.op1)
  466. else
  467. begin
  468. s:=getopstr(pai386(hp)^.op1t,pai386(hp)^.op1,pai386(hp)^.size,pai386(hp)^._operator,false);
  469. if pai386(hp)^.op3t<>top_none then
  470. begin
  471. if pai386(hp)^.op2t<>top_none then
  472. s:=getopstr(pai386(hp)^.op2t,pointer(longint(twowords(pai386(hp)^.op2).word1)),
  473. pai386(hp)^.size,pai386(hp)^._operator,true)+','+s;
  474. s:=getopstr(pai386(hp)^.op3t,pointer(longint(twowords(pai386(hp)^.op2).word2)),
  475. pai386(hp)^.size,pai386(hp)^._operator,false)+','+s;
  476. end
  477. else
  478. if pai386(hp)^.op2t<>top_none then
  479. s:=getopstr(pai386(hp)^.op2t,pai386(hp)^.op2,pai386(hp)^.size,
  480. pai386(hp)^._operator,true)+','+s;
  481. end;
  482. s:=#9+s;
  483. end
  484. else
  485. begin
  486. { check if string instruction }
  487. { long form, otherwise may give range check errors }
  488. { in turbo pascal... }
  489. if ((pai386(hp)^._operator = A_CMPS) or
  490. (pai386(hp)^._operator = A_INS) or
  491. (pai386(hp)^._operator = A_OUTS) or
  492. (pai386(hp)^._operator = A_SCAS) or
  493. (pai386(hp)^._operator = A_STOS) or
  494. (pai386(hp)^._operator = A_MOVS) or
  495. (pai386(hp)^._operator = A_LODS) or
  496. (pai386(hp)^._operator = A_XLAT)) then
  497. Begin
  498. case pai386(hp)^.size of
  499. S_B: suffix:='b';
  500. S_W: suffix:='w';
  501. S_L: suffix:='d';
  502. else
  503. Message(assem_f_invalid_suffix_intel);
  504. end;
  505. end;
  506. s:='';
  507. end;
  508. if pai386(hp)^._operator=A_FWAIT then
  509. AsmWriteln(#9#9'DB'#9'09bh')
  510. else
  511. AsmWriteLn(#9#9+prefix+int_op2str[pai386(hp)^._operator]+suffix+s);
  512. end;
  513. {$ifdef GDB}
  514. ait_stabn,
  515. ait_stabs,
  516. ait_stab_function_name : ;
  517. {$endif GDB}
  518. ait_marker : ;
  519. else
  520. internalerror(10000);
  521. end;
  522. hp:=pai(hp^.next);
  523. end;
  524. end;
  525. procedure ti386nasmasmlist.WriteAsmList;
  526. begin
  527. {$ifdef EXTDEBUG}
  528. if assigned(current_module^.mainsource) then
  529. comment(v_info,'Start writing nasm-styled assembler output for '+current_module^.mainsource^);
  530. {$endif}
  531. LastSec:=sec_none;
  532. AsmWriteLn('BITS 32');
  533. AsmLn;
  534. countlabelref:=false;
  535. WriteTree(externals);
  536. { Nasm doesn't support stabs
  537. WriteTree(debuglist);}
  538. WriteTree(codesegment);
  539. WriteTree(datasegment);
  540. WriteTree(consts);
  541. WriteTree(rttilist);
  542. WriteTree(bsssegment);
  543. countlabelref:=true;
  544. AsmLn;
  545. {$ifdef EXTDEBUG}
  546. if assigned(current_module^.mainsource) then
  547. comment(v_info,'Done writing nasm-styled assembler output for '+current_module^.mainsource^);
  548. {$endif EXTDEBUG}
  549. end;
  550. end.
  551. {
  552. $Log$
  553. Revision 1.11 1998-10-12 12:20:42 pierre
  554. + added tai_const_symbol_offset
  555. for r : pointer = @var.field;
  556. * better message for different arg names on implementation
  557. of function
  558. Revision 1.10 1998/10/06 17:16:34 pierre
  559. * some memory leaks fixed (thanks to Peter for heaptrc !)
  560. Revision 1.9 1998/10/01 20:19:07 jonas
  561. + ait_marker support
  562. Revision 1.8 1998/09/20 17:11:22 jonas
  563. * released REGALLOC
  564. Revision 1.7 1998/08/11 14:01:43 peter
  565. * fixed fwait bug using direct opcode
  566. Revision 1.6 1998/08/10 15:49:39 peter
  567. * small fixes for 0.99.5
  568. Revision 1.5 1998/08/08 10:19:18 florian
  569. * small fixes to write the extended type correct
  570. Revision 1.4 1998/06/05 17:46:03 peter
  571. * tp doesn't like comp() typecast
  572. Revision 1.3 1998/05/28 17:24:27 peter
  573. - $R- for tp to solve range errors with in[]
  574. Revision 1.2 1998/05/25 17:11:37 pierre
  575. * firstpasscount bug fixed
  576. now all is already set correctly the first time
  577. under EXTDEBUG try -gp to skip all other firstpasses
  578. it works !!
  579. * small bug fixes
  580. - for smallsets with -dTESTSMALLSET
  581. - some warnings removed (by correcting code !)
  582. Revision 1.1 1998/05/23 01:20:56 peter
  583. + aktasmmode, aktoptprocessor, aktoutputformat
  584. + smartlink per module $SMARTLINK-/+ (like MMX) and moved to aktswitches
  585. + $LIBNAME to set the library name where the unit will be put in
  586. * splitted cgi386 a bit (codeseg to large for bp7)
  587. * nasm, tasm works again. nasm moved to ag386nsm.pas
  588. }