ag386nsm.pas 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. unit ag386nsm;
  20. {$i defines.inc}
  21. interface
  22. uses aasm,assemble;
  23. type
  24. T386NasmAssembler = class(texternalassembler)
  25. procedure WriteTree(p:taasmoutput);override;
  26. procedure WriteAsmList;override;
  27. procedure WriteExternals;
  28. end;
  29. implementation
  30. uses
  31. {$ifdef delphi}
  32. sysutils,
  33. {$endif}
  34. cutils,globtype,globals,systems,cobjects,
  35. fmodule,finput,verbose,cpubase,cpuasm
  36. ;
  37. const
  38. line_length = 64;
  39. var
  40. lastfileinfo : tfileposinfo;
  41. infile,
  42. lastinfile : tinputfile;
  43. function fixline(s:string):string;
  44. {
  45. return s with all leading and ending spaces and tabs removed
  46. }
  47. var
  48. i,j,k : longint;
  49. begin
  50. i:=length(s);
  51. while (i>0) and (s[i] in [#9,' ']) do
  52. dec(i);
  53. j:=1;
  54. while (j<i) and (s[j] in [#9,' ']) do
  55. inc(j);
  56. for k:=j to i do
  57. if s[k] in [#0..#31,#127..#255] then
  58. s[k]:='.';
  59. fixline:=Copy(s,j,i-j+1);
  60. end;
  61. function single2str(d : single) : string;
  62. var
  63. hs : string;
  64. p : byte;
  65. begin
  66. str(d,hs);
  67. { nasm expects a lowercase e }
  68. p:=pos('E',hs);
  69. if p>0 then
  70. hs[p]:='e';
  71. p:=pos('+',hs);
  72. if p>0 then
  73. delete(hs,p,1);
  74. single2str:=lower(hs);
  75. end;
  76. function double2str(d : double) : string;
  77. var
  78. hs : string;
  79. p : byte;
  80. begin
  81. str(d,hs);
  82. { nasm expects a lowercase e }
  83. p:=pos('E',hs);
  84. if p>0 then
  85. hs[p]:='e';
  86. p:=pos('+',hs);
  87. if p>0 then
  88. delete(hs,p,1);
  89. double2str:=lower(hs);
  90. end;
  91. function extended2str(e : extended) : string;
  92. var
  93. hs : string;
  94. p : byte;
  95. begin
  96. str(e,hs);
  97. { nasm expects a lowercase e }
  98. p:=pos('E',hs);
  99. if p>0 then
  100. hs[p]:='e';
  101. p:=pos('+',hs);
  102. if p>0 then
  103. delete(hs,p,1);
  104. extended2str:=lower(hs);
  105. end;
  106. function comp2str(d : bestreal) : string;
  107. type
  108. pdouble = ^double;
  109. var
  110. c : comp;
  111. dd : pdouble;
  112. begin
  113. {$ifdef FPC}
  114. c:=comp(d);
  115. {$else}
  116. c:=d;
  117. {$endif}
  118. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  119. comp2str:=double2str(dd^);
  120. end;
  121. function getreferencestring(var ref : treference) : string;
  122. var
  123. s : string;
  124. first : boolean;
  125. begin
  126. if ref.is_immediate then
  127. begin
  128. getreferencestring:=tostr(ref.offset);
  129. exit;
  130. end
  131. else
  132. with ref do
  133. begin
  134. first:=true;
  135. inc(offset,offsetfixup);
  136. offsetfixup:=0;
  137. if ref.segment<>R_NO then
  138. s:='['+int_reg2str[segment]+':'
  139. else
  140. s:='[';
  141. if assigned(symbol) then
  142. begin
  143. s:=s+symbol^.name;
  144. first:=false;
  145. end;
  146. if (base<>R_NO) then
  147. begin
  148. if not(first) then
  149. s:=s+'+'
  150. else
  151. first:=false;
  152. s:=s+int_reg2str[base];
  153. end;
  154. if (index<>R_NO) then
  155. begin
  156. if not(first) then
  157. s:=s+'+'
  158. else
  159. first:=false;
  160. s:=s+int_reg2str[index];
  161. if scalefactor<>0 then
  162. s:=s+'*'+tostr(scalefactor);
  163. end;
  164. if offset<0 then
  165. s:=s+tostr(offset)
  166. else if (offset>0) then
  167. s:=s+'+'+tostr(offset);
  168. s:=s+']';
  169. end;
  170. getreferencestring:=s;
  171. end;
  172. function sizestr(s:topsize;dest:boolean):string;
  173. begin
  174. case s of
  175. S_B : sizestr:='byte ';
  176. S_W : sizestr:='word ';
  177. S_L : sizestr:='dword ';
  178. S_IS : sizestr:='word ';
  179. S_IL : sizestr:='dword ';
  180. S_IQ : sizestr:='qword ';
  181. S_FS : sizestr:='dword ';
  182. S_FL : sizestr:='qword ';
  183. S_FX : sizestr:='tword ';
  184. S_BW : if dest then
  185. sizestr:='word '
  186. else
  187. sizestr:='byte ';
  188. S_BL : if dest then
  189. sizestr:='dword '
  190. else
  191. sizestr:='byte ';
  192. S_WL : if dest then
  193. sizestr:='dword '
  194. else
  195. sizestr:='word ';
  196. else { S_NO }
  197. sizestr:='';
  198. end;
  199. end;
  200. function getopstr(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean) : string;
  201. var
  202. hs : string;
  203. begin
  204. case o.typ of
  205. top_reg :
  206. getopstr:=int_nasmreg2str[o.reg];
  207. top_const :
  208. begin
  209. if (ops=1) and (opcode<>A_RET) then
  210. getopstr:=sizestr(s,dest)+tostr(o.val)
  211. else
  212. getopstr:=tostr(o.val);
  213. end;
  214. top_symbol :
  215. begin
  216. if assigned(o.sym) then
  217. hs:='dword '+o.sym^.name
  218. else
  219. hs:='dword ';
  220. if o.symofs>0 then
  221. hs:=hs+'+'+tostr(o.symofs)
  222. else
  223. if o.symofs<0 then
  224. hs:=hs+tostr(o.symofs)
  225. else
  226. if not(assigned(o.sym)) then
  227. hs:=hs+'0';
  228. getopstr:=hs;
  229. end;
  230. top_ref :
  231. begin
  232. hs:=getreferencestring(o.ref^);
  233. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  234. (opcode = A_LSS) or (opcode = A_LFS) or
  235. (opcode = A_LES) or (opcode = A_LDS) or
  236. (opcode = A_SHR) or (opcode = A_SHL) or
  237. (opcode = A_SAR) or (opcode = A_SAL) or
  238. (opcode = A_OUT) or (opcode = A_IN)) then
  239. begin
  240. hs:=sizestr(s,dest)+hs;
  241. end;
  242. getopstr:=hs;
  243. end;
  244. else
  245. internalerror(10001);
  246. end;
  247. end;
  248. function getopstr_jmp(const o:toper; op : tasmop) : string;
  249. var
  250. hs : string;
  251. begin
  252. case o.typ of
  253. top_reg :
  254. getopstr_jmp:=int_nasmreg2str[o.reg];
  255. top_ref :
  256. getopstr_jmp:=getreferencestring(o.ref^);
  257. top_const :
  258. getopstr_jmp:=tostr(o.val);
  259. top_symbol :
  260. begin
  261. hs:=o.sym^.name;
  262. if o.symofs>0 then
  263. hs:=hs+'+'+tostr(o.symofs)
  264. else
  265. if o.symofs<0 then
  266. hs:=hs+tostr(o.symofs);
  267. if (op=A_JCXZ) or (op=A_JECXZ) or
  268. (op=A_LOOP) or (op=A_LOOPE) or
  269. (op=A_LOOPNE) or (op=A_LOOPNZ) or
  270. (op=A_LOOPZ) then
  271. getopstr_jmp:=hs
  272. else
  273. getopstr_jmp:='NEAR '+hs;
  274. end;
  275. else
  276. internalerror(10001);
  277. end;
  278. end;
  279. {****************************************************************************
  280. T386NasmAssembler
  281. ****************************************************************************}
  282. var
  283. LastSec : tsection;
  284. const
  285. ait_const2str:array[ait_const_32bit..ait_const_8bit] of string[8]=
  286. (#9'DD'#9,#9'DW'#9,#9'DB'#9);
  287. Function PadTabs(const p:string;addch:char):string;
  288. var
  289. s : string;
  290. i : longint;
  291. begin
  292. i:=length(p);
  293. if addch<>#0 then
  294. begin
  295. inc(i);
  296. s:=p+addch;
  297. end
  298. else
  299. s:=p;
  300. if i<8 then
  301. PadTabs:=s+#9#9
  302. else
  303. PadTabs:=s+#9;
  304. end;
  305. procedure T386NasmAssembler.WriteTree(p:taasmoutput);
  306. const
  307. allocstr : array[boolean] of string[10]=(' released',' allocated');
  308. nolinetai =[ait_label,
  309. ait_regalloc,ait_tempalloc,
  310. ait_stabn,ait_stabs,ait_section,
  311. ait_cut,ait_marker,ait_align,ait_stab_function_name];
  312. var
  313. s : string;
  314. {prefix,
  315. suffix : string; no need here }
  316. hp : tai;
  317. counter,
  318. lines,
  319. i,j,l : longint;
  320. InlineLevel : longint;
  321. consttyp : tait;
  322. found,
  323. do_line,
  324. quoted : boolean;
  325. sep : char;
  326. begin
  327. if not assigned(p) then
  328. exit;
  329. InlineLevel:=0;
  330. { lineinfo is only needed for codesegment (PFV) }
  331. do_line:=(cs_asm_source in aktglobalswitches) or
  332. ((cs_lineinfo in aktmoduleswitches)
  333. and (p=codesegment));
  334. hp:=tai(p.first);
  335. while assigned(hp) do
  336. begin
  337. aktfilepos:=hp.fileinfo;
  338. if not(hp.typ in nolinetai) then
  339. begin
  340. if do_line then
  341. begin
  342. { load infile }
  343. if lastfileinfo.fileindex<>hp.fileinfo.fileindex then
  344. begin
  345. infile:=current_module.sourcefiles.get_file(hp.fileinfo.fileindex);
  346. if assigned(infile) then
  347. begin
  348. { open only if needed !! }
  349. if (cs_asm_source in aktglobalswitches) then
  350. infile.open;
  351. end;
  352. { avoid unnecessary reopens of the same file !! }
  353. lastfileinfo.fileindex:=hp.fileinfo.fileindex;
  354. { be sure to change line !! }
  355. lastfileinfo.line:=-1;
  356. end;
  357. { write source }
  358. if (cs_asm_source in aktglobalswitches) and
  359. assigned(infile) then
  360. begin
  361. if (infile<>lastinfile) then
  362. begin
  363. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  364. if assigned(lastinfile) then
  365. lastinfile.close;
  366. end;
  367. if (hp.fileinfo.line<>lastfileinfo.line) and
  368. ((hp.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  369. begin
  370. if (hp.fileinfo.line<>0) and
  371. ((infile.linebuf^[hp.fileinfo.line]>=0) or (InlineLevel>0)) then
  372. AsmWriteLn(target_asm.comment+'['+tostr(hp.fileinfo.line)+'] '+
  373. fixline(infile.GetLineStr(hp.fileinfo.line)));
  374. { set it to a negative value !
  375. to make that is has been read already !! PM }
  376. if (infile.linebuf^[hp.fileinfo.line]>=0) then
  377. infile.linebuf^[hp.fileinfo.line]:=-infile.linebuf^[hp.fileinfo.line]-1;
  378. end;
  379. end;
  380. lastfileinfo:=hp.fileinfo;
  381. lastinfile:=infile;
  382. end;
  383. end;
  384. case hp.typ of
  385. ait_comment :
  386. Begin
  387. AsmWrite(target_asm.comment);
  388. AsmWritePChar(tai_asm_comment(hp).str);
  389. AsmLn;
  390. End;
  391. ait_regalloc :
  392. begin
  393. if (cs_asm_regalloc in aktglobalswitches) then
  394. AsmWriteLn(target_asm.comment+'Register '+att_reg2str[tairegalloc(hp).reg]+
  395. allocstr[tairegalloc(hp).allocation]);
  396. end;
  397. ait_tempalloc :
  398. begin
  399. if (cs_asm_tempalloc in aktglobalswitches) then
  400. AsmWriteLn(target_asm.comment+'Temp '+tostr(taitempalloc(hp).temppos)+','+
  401. tostr(taitempalloc(hp).tempsize)+allocstr[taitempalloc(hp).allocation]);
  402. end;
  403. ait_section :
  404. begin
  405. if tai_section(hp).sec<>sec_none then
  406. begin
  407. AsmLn;
  408. AsmWriteLn('SECTION '+target_asm.secnames[tai_section(hp).sec]);
  409. end;
  410. LastSec:=tai_section(hp).sec;
  411. end;
  412. ait_align :
  413. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  414. ait_datablock :
  415. begin
  416. if tai_datablock(hp).is_global then
  417. begin
  418. AsmWrite(#9'GLOBAL ');
  419. AsmWriteLn(tai_datablock(hp).sym^.name);
  420. end;
  421. AsmWrite(PadTabs(tai_datablock(hp).sym^.name,':'));
  422. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  423. end;
  424. ait_const_32bit,
  425. ait_const_16bit,
  426. ait_const_8bit :
  427. begin
  428. AsmWrite(ait_const2str[hp.typ]+tostr(tai_const(hp).value));
  429. consttyp:=hp.typ;
  430. l:=0;
  431. repeat
  432. found:=(not (tai(hp.next)=nil)) and (tai(hp.next).typ=consttyp);
  433. if found then
  434. begin
  435. hp:=tai(hp.next);
  436. s:=','+tostr(tai_const(hp).value);
  437. AsmWrite(s);
  438. inc(l,length(s));
  439. end;
  440. until (not found) or (l>line_length);
  441. AsmLn;
  442. end;
  443. ait_const_symbol :
  444. begin
  445. AsmWrite(#9#9'DD'#9);
  446. AsmWrite(tai_const_symbol(hp).sym^.name);
  447. if tai_const_symbol(hp).offset>0 then
  448. AsmWrite('+'+tostr(tai_const_symbol(hp).offset))
  449. else if tai_const_symbol(hp).offset<0 then
  450. AsmWrite(tostr(tai_const_symbol(hp).offset));
  451. AsmLn;
  452. end;
  453. ait_const_rva :
  454. begin
  455. AsmWrite(#9#9'RVA'#9);
  456. AsmWriteLn(tai_const_symbol(hp).sym^.name);
  457. end;
  458. ait_real_32bit :
  459. AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  460. ait_real_64bit :
  461. AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  462. ait_real_80bit :
  463. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  464. ait_comp_64bit :
  465. AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  466. ait_string :
  467. begin
  468. counter := 0;
  469. lines := tai_string(hp).len div line_length;
  470. { separate lines in different parts }
  471. if tai_string(hp).len > 0 then
  472. Begin
  473. for j := 0 to lines-1 do
  474. begin
  475. AsmWrite(#9#9'DB'#9);
  476. quoted:=false;
  477. for i:=counter to counter+line_length-1 do
  478. begin
  479. { it is an ascii character. }
  480. if (ord(tai_string(hp).str[i])>31) and
  481. (ord(tai_string(hp).str[i])<128) and
  482. (tai_string(hp).str[i]<>'"') then
  483. begin
  484. if not(quoted) then
  485. begin
  486. if i>counter then
  487. AsmWrite(',');
  488. AsmWrite('"');
  489. end;
  490. AsmWrite(tai_string(hp).str[i]);
  491. quoted:=true;
  492. end { if > 31 and < 128 and ord('"') }
  493. else
  494. begin
  495. if quoted then
  496. AsmWrite('"');
  497. if i>counter then
  498. AsmWrite(',');
  499. quoted:=false;
  500. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  501. end;
  502. end; { end for i:=0 to... }
  503. if quoted then AsmWrite('"');
  504. AsmWrite(target_os.newline);
  505. inc(counter,line_length);
  506. end; { end for j:=0 ... }
  507. { do last line of lines }
  508. if counter<tai_string(hp).len then
  509. AsmWrite(#9#9'DB'#9);
  510. quoted:=false;
  511. for i:=counter to tai_string(hp).len-1 do
  512. begin
  513. { it is an ascii character. }
  514. if (ord(tai_string(hp).str[i])>31) and
  515. (ord(tai_string(hp).str[i])<128) and
  516. (tai_string(hp).str[i]<>'"') then
  517. begin
  518. if not(quoted) then
  519. begin
  520. if i>counter then
  521. AsmWrite(',');
  522. AsmWrite('"');
  523. end;
  524. AsmWrite(tai_string(hp).str[i]);
  525. quoted:=true;
  526. end { if > 31 and < 128 and " }
  527. else
  528. begin
  529. if quoted then
  530. AsmWrite('"');
  531. if i>counter then
  532. AsmWrite(',');
  533. quoted:=false;
  534. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  535. end;
  536. end; { end for i:=0 to... }
  537. if quoted then
  538. AsmWrite('"');
  539. end;
  540. AsmLn;
  541. end;
  542. ait_label :
  543. begin
  544. if tai_label(hp).l^.is_used then
  545. AsmWriteLn(tai_label(hp).l^.name+':');
  546. end;
  547. ait_direct :
  548. begin
  549. AsmWritePChar(tai_direct(hp).str);
  550. AsmLn;
  551. end;
  552. ait_symbol :
  553. begin
  554. if tai_symbol(hp).is_global then
  555. begin
  556. AsmWrite(#9'GLOBAL ');
  557. AsmWriteLn(tai_symbol(hp).sym^.name);
  558. end;
  559. AsmWrite(tai_symbol(hp).sym^.name);
  560. if assigned(hp.next) and not(tai(hp.next).typ in
  561. [ait_const_32bit,ait_const_16bit,ait_const_8bit,
  562. ait_const_symbol,ait_const_rva,
  563. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  564. AsmWriteLn(':')
  565. end;
  566. ait_symbol_end : ;
  567. ait_instruction :
  568. begin
  569. { Must be done with args in ATT order }
  570. taicpu(hp).SetOperandOrder(op_att);
  571. taicpu(hp).CheckNonCommutativeOpcodes;
  572. { We need intel order, no At&t }
  573. taicpu(hp).SetOperandOrder(op_intel);
  574. { Reset
  575. suffix:='';
  576. prefix:='';}
  577. s:='';
  578. if ((taicpu(hp).opcode=A_FADDP) or
  579. (taicpu(hp).opcode=A_FMULP))
  580. and (taicpu(hp).ops=0) then
  581. begin
  582. taicpu(hp).ops:=2;
  583. taicpu(hp).oper[0].typ:=top_reg;
  584. taicpu(hp).oper[0].reg:=R_ST1;
  585. taicpu(hp).oper[1].typ:=top_reg;
  586. taicpu(hp).oper[1].reg:=R_ST;
  587. end;
  588. if taicpu(hp).ops<>0 then
  589. begin
  590. if is_calljmp(taicpu(hp).opcode) then
  591. s:=#9+getopstr_jmp(taicpu(hp).oper[0],taicpu(hp).opcode)
  592. else
  593. begin
  594. { We need to explicitely set
  595. word prefix to get selectors
  596. to be pushed in 2 bytes PM }
  597. if (taicpu(hp).opsize=S_W) and
  598. ((taicpu(hp).opcode=A_PUSH) or
  599. (taicpu(hp).opcode=A_POP)) and
  600. (taicpu(hp).oper[0].typ=top_reg) and
  601. ((taicpu(hp).oper[0].reg>=firstsreg) and
  602. (taicpu(hp).oper[0].reg<=lastsreg)) then
  603. AsmWriteln(#9#9'DB'#9'066h');
  604. for i:=0 to taicpu(hp).ops-1 do
  605. begin
  606. if i=0 then
  607. sep:=#9
  608. else
  609. sep:=',';
  610. s:=s+sep+getopstr(taicpu(hp).oper[i],taicpu(hp).opsize,taicpu(hp).opcode,
  611. taicpu(hp).ops,(i=2));
  612. end;
  613. end;
  614. end;
  615. if taicpu(hp).opcode=A_FWAIT then
  616. AsmWriteln(#9#9'DB'#9'09bh')
  617. else
  618. AsmWriteLn(#9#9+{prefix+}int_op2str[taicpu(hp).opcode]+
  619. cond2str[taicpu(hp).condition]+{suffix+}s);
  620. end;
  621. {$ifdef GDB}
  622. ait_stabn,
  623. ait_stabs,
  624. ait_force_line,
  625. ait_stab_function_name : ;
  626. {$endif GDB}
  627. ait_cut :
  628. begin
  629. { only reset buffer if nothing has changed }
  630. if AsmSize=AsmStartSize then
  631. AsmClear
  632. else
  633. begin
  634. AsmClose;
  635. DoAssemble;
  636. AsmCreate(tai_cut(hp).place);
  637. end;
  638. { avoid empty files }
  639. while assigned(hp.next) and (tai(hp.next).typ in [ait_cut,ait_section,ait_comment]) do
  640. begin
  641. if tai(hp.next).typ=ait_section then
  642. lastsec:=tai_section(hp.next).sec;
  643. hp:=tai(hp.next);
  644. end;
  645. if lastsec<>sec_none then
  646. AsmWriteLn('SECTION '+target_asm.secnames[lastsec]);
  647. AsmStartSize:=AsmSize;
  648. end;
  649. ait_marker :
  650. if tai_marker(hp).kind=InlineStart then
  651. inc(InlineLevel)
  652. else if tai_marker(hp).kind=InlineEnd then
  653. dec(InlineLevel);
  654. else
  655. internalerror(10000);
  656. end;
  657. hp:=tai(hp.next);
  658. end;
  659. end;
  660. var
  661. currentasmlist : TExternalAssembler;
  662. procedure writeexternal(p:pnamedindexobject);
  663. begin
  664. if pasmsymbol(p)^.defbind=AB_EXTERNAL then
  665. currentasmlist.AsmWriteln('EXTERN'#9+p^.name);
  666. end;
  667. procedure T386NasmAssembler.WriteExternals;
  668. begin
  669. currentasmlist:=self;
  670. AsmSymbolList^.foreach({$ifdef fpcprocvar}@{$endif}writeexternal);
  671. end;
  672. procedure T386NasmAssembler.WriteAsmList;
  673. begin
  674. {$ifdef EXTDEBUG}
  675. if assigned(current_module.mainsource) then
  676. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource^);
  677. {$endif}
  678. LastSec:=sec_none;
  679. AsmWriteLn('BITS 32');
  680. AsmLn;
  681. countlabelref:=false;
  682. lastfileinfo.line:=-1;
  683. lastfileinfo.fileindex:=0;
  684. lastinfile:=nil;
  685. WriteExternals;
  686. { Nasm doesn't support stabs
  687. WriteTree(debuglist);}
  688. WriteTree(codesegment);
  689. WriteTree(datasegment);
  690. WriteTree(consts);
  691. WriteTree(rttilist);
  692. WriteTree(resourcestringlist);
  693. WriteTree(bsssegment);
  694. Writetree(importssection);
  695. { exports are written by DLLTOOL
  696. if we use it so don't insert it twice (PM) }
  697. if not UseDeffileForExport and assigned(exportssection) then
  698. Writetree(exportssection);
  699. Writetree(resourcesection);
  700. countlabelref:=true;
  701. AsmLn;
  702. {$ifdef EXTDEBUG}
  703. if assigned(current_module.mainsource) then
  704. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource^);
  705. {$endif EXTDEBUG}
  706. end;
  707. end.
  708. {
  709. $Log$
  710. Revision 1.6 2001-03-05 21:39:11 peter
  711. * changed to class with common TAssembler also for internal assembler
  712. Revision 1.5 2001/02/20 21:36:39 peter
  713. * tasm/masm fixes merged
  714. Revision 1.4 2001/01/13 20:24:24 peter
  715. * fixed operand order that got mixed up for external writers after
  716. my previous assembler block valid instruction check
  717. Revision 1.3 2000/12/25 00:07:31 peter
  718. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  719. tlinkedlist objects)
  720. Revision 1.2 2000/11/29 00:30:43 florian
  721. * unused units removed from uses clause
  722. * some changes for widestrings
  723. Revision 1.1 2000/10/15 09:47:42 peter
  724. * moved to i386/
  725. Revision 1.6 2000/09/24 15:06:11 peter
  726. * use defines.inc
  727. Revision 1.5 2000/08/27 16:11:49 peter
  728. * moved some util functions from globals,cobjects to cutils
  729. * splitted files into finput,fmodule
  730. Revision 1.4 2000/08/20 17:38:21 peter
  731. * smartlinking fixed for linux (merged)
  732. Revision 1.3 2000/07/13 12:08:24 michael
  733. + patched to 1.1.0 with former 1.09patch from peter
  734. Revision 1.2 2000/07/13 11:32:30 michael
  735. + removed logs
  736. }