ag386nsm.pas 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. {
  2. Copyright (c) 1998-2002 by Florian Klaempfl
  3. This unit implements an asmoutput class for the Nasm assembler with
  4. Intel syntax for the i386+
  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 ag386nsm;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cpubase,
  23. aasmbase,aasmtai,aasmdata,aasmcpu,assemble,cgutils;
  24. type
  25. T386NasmAssembler = class(texternalassembler)
  26. private
  27. procedure WriteReference(var ref : treference);
  28. procedure WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  29. procedure WriteOper_jmp(const o:toper; op : tasmop);
  30. procedure WriteSection(atype:TAsmSectiontype;const aname:string);
  31. public
  32. procedure WriteTree(p:TAsmList);override;
  33. procedure WriteAsmList;override;
  34. procedure WriteExternals;
  35. procedure WriteSmartExternals;
  36. end;
  37. implementation
  38. uses
  39. cutils,globtype,globals,systems,cclasses,
  40. fmodule,finput,verbose,cpuinfo,cgbase
  41. ;
  42. type
  43. {$ifdef cpuextended}
  44. t80bitarray = array[0..9] of byte;
  45. {$endif cpuextended}
  46. t64bitarray = array[0..7] of byte;
  47. t32bitarray = array[0..3] of byte;
  48. const
  49. line_length = 64;
  50. nasm_regname_table : array[tregisterindex] of string[7] = (
  51. {r386nasm.inc contains the Nasm name of each register.}
  52. {$i r386nasm.inc}
  53. );
  54. function nasm_regname(r:Tregister):string;
  55. var
  56. p : tregisterindex;
  57. begin
  58. p:=findreg_by_number(r);
  59. if p<>0 then
  60. result:=nasm_regname_table[p]
  61. else
  62. result:=generic_regname(r);
  63. end;
  64. function fixline(s:string):string;
  65. {
  66. return s with all leading and ending spaces and tabs removed
  67. }
  68. var
  69. i,j,k : longint;
  70. begin
  71. i:=length(s);
  72. while (i>0) and (s[i] in [#9,' ']) do
  73. dec(i);
  74. j:=1;
  75. while (j<i) and (s[j] in [#9,' ']) do
  76. inc(j);
  77. for k:=j to i do
  78. if s[k] in [#0..#31,#127..#255] then
  79. s[k]:='.';
  80. fixline:=Copy(s,j,i-j+1);
  81. end;
  82. function single2str(d : single) : string;
  83. var
  84. hs : string;
  85. p : longint;
  86. begin
  87. str(d,hs);
  88. { nasm expects a lowercase e }
  89. p:=pos('E',hs);
  90. if p>0 then
  91. hs[p]:='e';
  92. p:=pos('+',hs);
  93. if p>0 then
  94. delete(hs,p,1);
  95. single2str:=lower(hs);
  96. end;
  97. function double2str(d : double) : string;
  98. var
  99. hs : string;
  100. p : longint;
  101. begin
  102. str(d,hs);
  103. { nasm expects a lowercase e }
  104. p:=pos('E',hs);
  105. if p>0 then
  106. hs[p]:='e';
  107. p:=pos('+',hs);
  108. if p>0 then
  109. delete(hs,p,1);
  110. double2str:=lower(hs);
  111. end;
  112. function extended2str(e : extended) : string;
  113. var
  114. hs : string;
  115. p : longint;
  116. begin
  117. str(e,hs);
  118. { nasm expects a lowercase e }
  119. p:=pos('E',hs);
  120. if p>0 then
  121. hs[p]:='e';
  122. p:=pos('+',hs);
  123. if p>0 then
  124. delete(hs,p,1);
  125. extended2str:=lower(hs);
  126. end;
  127. { convert floating point values }
  128. { to correct endian }
  129. procedure swap64bitarray(var t: t64bitarray);
  130. var
  131. b: byte;
  132. begin
  133. b:= t[7];
  134. t[7] := t[0];
  135. t[0] := b;
  136. b := t[6];
  137. t[6] := t[1];
  138. t[1] := b;
  139. b:= t[5];
  140. t[5] := t[2];
  141. t[2] := b;
  142. b:= t[4];
  143. t[4] := t[3];
  144. t[3] := b;
  145. end;
  146. procedure swap32bitarray(var t: t32bitarray);
  147. var
  148. b: byte;
  149. begin
  150. b:= t[1];
  151. t[1]:= t[2];
  152. t[2]:= b;
  153. b:= t[0];
  154. t[0]:= t[3];
  155. t[3]:= b;
  156. end;
  157. function comp2str(d : bestreal) : string;
  158. type
  159. pdouble = ^double;
  160. var
  161. c : comp;
  162. dd : pdouble;
  163. begin
  164. c:=comp(d);
  165. dd:=pdouble(@c); { this makes a bitwise copy of c into a double }
  166. comp2str:=double2str(dd^);
  167. end;
  168. function sizestr(s:topsize;dest:boolean):string;
  169. begin
  170. case s of
  171. S_B : sizestr:='byte ';
  172. S_W : sizestr:='word ';
  173. S_L : sizestr:='dword ';
  174. S_Q : sizestr:='qword ';
  175. S_IS : sizestr:='word ';
  176. S_IL : sizestr:='dword ';
  177. S_IQ : sizestr:='qword ';
  178. S_FS : sizestr:='dword ';
  179. S_FL : sizestr:='qword ';
  180. S_FX : sizestr:='tword ';
  181. S_BW : if dest then
  182. sizestr:='word '
  183. else
  184. sizestr:='byte ';
  185. S_BL : if dest then
  186. sizestr:='dword '
  187. else
  188. sizestr:='byte ';
  189. S_WL : if dest then
  190. sizestr:='dword '
  191. else
  192. sizestr:='word ';
  193. else { S_NO }
  194. sizestr:='';
  195. end;
  196. end;
  197. Function PadTabs(const p:string;addch:char):string;
  198. var
  199. s : string;
  200. i : longint;
  201. begin
  202. i:=length(p);
  203. if addch<>#0 then
  204. begin
  205. inc(i);
  206. s:=p+addch;
  207. end
  208. else
  209. s:=p;
  210. if i<8 then
  211. PadTabs:=s+#9#9
  212. else
  213. PadTabs:=s+#9;
  214. end;
  215. type
  216. PExternChain = ^TExternChain;
  217. TExternChain = Record
  218. psym : pshortstring;
  219. is_defined : boolean;
  220. next : PExternChain;
  221. end;
  222. const
  223. FEC : PExternChain = nil;
  224. procedure AddSymbol(symname : string; defined : boolean);
  225. var
  226. EC : PExternChain;
  227. begin
  228. EC:=FEC;
  229. while assigned(EC) do
  230. begin
  231. if EC^.psym^=symname then
  232. begin
  233. if defined then
  234. EC^.is_defined:=true;
  235. exit;
  236. end;
  237. EC:=EC^.next;
  238. end;
  239. New(EC);
  240. EC^.next:=FEC;
  241. FEC:=EC;
  242. FEC^.psym:=stringdup(symname);
  243. FEC^.is_defined := defined;
  244. end;
  245. procedure FreeExternChainList;
  246. var
  247. EC : PExternChain;
  248. begin
  249. EC:=FEC;
  250. while assigned(EC) do
  251. begin
  252. FEC:=EC^.next;
  253. stringdispose(EC^.psym);
  254. Dispose(EC);
  255. EC:=FEC;
  256. end;
  257. end;
  258. {****************************************************************************
  259. T386NasmAssembler
  260. ****************************************************************************}
  261. procedure T386NasmAssembler.WriteReference(var ref : treference);
  262. var
  263. first : boolean;
  264. begin
  265. with ref do
  266. begin
  267. AsmWrite('[');
  268. first:=true;
  269. if (segment<>NR_NO) then
  270. AsmWrite(nasm_regname(segment)+':');
  271. if assigned(symbol) then
  272. begin
  273. AsmWrite(symbol.name);
  274. if SmartAsm then
  275. AddSymbol(symbol.name,false);
  276. first:=false;
  277. end;
  278. if (base<>NR_NO) then
  279. begin
  280. if not(first) then
  281. AsmWrite('+')
  282. else
  283. first:=false;
  284. AsmWrite(nasm_regname(base))
  285. end;
  286. if (index<>NR_NO) then
  287. begin
  288. if not(first) then
  289. AsmWrite('+')
  290. else
  291. first:=false;
  292. AsmWrite(nasm_regname(index));
  293. if scalefactor<>0 then
  294. AsmWrite('*'+tostr(scalefactor));
  295. end;
  296. if offset<0 then
  297. begin
  298. AsmWrite(tostr(offset));
  299. first:=false;
  300. end
  301. else if (offset>0) then
  302. begin
  303. AsmWrite('+'+tostr(offset));
  304. first:=false;
  305. end;
  306. if first then
  307. AsmWrite('0');
  308. AsmWrite(']');
  309. end;
  310. end;
  311. procedure T386NasmAssembler.WriteOper(const o:toper;s : topsize; opcode: tasmop;ops:longint;dest : boolean);
  312. begin
  313. case o.typ of
  314. top_reg :
  315. AsmWrite(nasm_regname(o.reg));
  316. top_const :
  317. begin
  318. if (ops=1) and (opcode<>A_RET) then
  319. AsmWrite(sizestr(s,dest));
  320. AsmWrite(tostr(longint(o.val)));
  321. end;
  322. top_ref :
  323. begin
  324. if o.ref^.refaddr=addr_no then
  325. begin
  326. if not ((opcode = A_LEA) or (opcode = A_LGS) or
  327. (opcode = A_LSS) or (opcode = A_LFS) or
  328. (opcode = A_LES) or (opcode = A_LDS) or
  329. // (opcode = A_SHR) or (opcode = A_SHL) or
  330. (opcode = A_SAR) or (opcode = A_SAL) or
  331. (opcode = A_OUT) or (opcode = A_IN)) then
  332. AsmWrite(sizestr(s,dest));
  333. WriteReference(o.ref^);
  334. end
  335. else
  336. begin
  337. asmwrite('dword ');
  338. if assigned(o.ref^.symbol) then
  339. begin
  340. if SmartAsm then
  341. AddSymbol(o.ref^.symbol.name,false);
  342. asmwrite(o.ref^.symbol.name);
  343. if o.ref^.offset=0 then
  344. exit;
  345. end;
  346. if o.ref^.offset>0 then
  347. asmwrite('+');
  348. asmwrite(tostr(o.ref^.offset));
  349. end;
  350. end;
  351. else
  352. internalerror(10001);
  353. end;
  354. end;
  355. procedure T386NasmAssembler.WriteOper_jmp(const o:toper; op : tasmop);
  356. begin
  357. case o.typ of
  358. top_reg :
  359. AsmWrite(nasm_regname(o.reg));
  360. top_ref :
  361. if o.ref^.refaddr=addr_no then
  362. WriteReference(o.ref^)
  363. else
  364. begin
  365. if not(
  366. (op=A_JCXZ) or (op=A_JECXZ) or
  367. {$ifdef x86_64}
  368. (op=A_JRCXZ) or
  369. {$endif x86_64}
  370. (op=A_LOOP) or (op=A_LOOPE) or
  371. (op=A_LOOPNE) or (op=A_LOOPNZ) or
  372. (op=A_LOOPZ)
  373. ) then
  374. AsmWrite('NEAR ');
  375. AsmWrite(o.ref^.symbol.name);
  376. if SmartAsm then
  377. AddSymbol(o.ref^.symbol.name,false);
  378. if o.ref^.offset>0 then
  379. AsmWrite('+'+tostr(o.ref^.offset))
  380. else
  381. if o.ref^.offset<0 then
  382. AsmWrite(tostr(o.ref^.offset));
  383. end;
  384. top_const :
  385. AsmWrite(tostr(aint(o.val)));
  386. else
  387. internalerror(10001);
  388. end;
  389. end;
  390. const
  391. ait_const2str : array[aitconst_128bit..aitconst_indirect_symbol] of string[20]=(
  392. #9'FIXME_128BIT'#9,#9'FIXME_64BIT'#9,#9'DD'#9,#9'DW'#9,#9'DB'#9,
  393. #9'FIXME_SLEB128BIT'#9,#9'FIXME_ULEB128BIT'#9,
  394. #9'RVA'#9,#9'SECREL32'#9,#9'FIXMEINDIRECT'#9
  395. );
  396. procedure T386NasmAssembler.WriteSection(atype:TAsmSectiontype;const aname:string);
  397. const
  398. secnames : array[TAsmSectiontype] of string[length('__DATA, __datacoal_nt,coalesced')] = ('',
  399. '.text',
  400. '.data',
  401. '.data',
  402. '.rodata',
  403. '.bss',
  404. '.tbss',
  405. '.pdata',
  406. '.text',
  407. '.stab',
  408. '.stabstr',
  409. '.idata2','.idata4','.idata5','.idata6','.idata7','.edata',
  410. '.eh_frame',
  411. '.debug_frame','.debug_info','.debug_line','.debug_abbrev',
  412. '.fpc',
  413. '',
  414. '.init',
  415. '.fini',
  416. '.objc_class',
  417. '.objc_meta_class',
  418. '.objc_cat_cls_meth',
  419. '.objc_cat_inst_meth',
  420. '.objc_protocol',
  421. '.objc_string_object',
  422. '.objc_cls_meth',
  423. '.objc_inst_meth',
  424. '.objc_cls_refs',
  425. '.objc_message_refs',
  426. '.objc_symbols',
  427. '.objc_category',
  428. '.objc_class_vars',
  429. '.objc_instance_vars',
  430. '.objc_module_info',
  431. '.objc_class_names',
  432. '.objc_meth_var_types',
  433. '.objc_meth_var_names',
  434. '.objc_selector_strs',
  435. '.objc_protocol_ext',
  436. '.objc_class_ext',
  437. '.objc_property',
  438. '.objc_image_info',
  439. '.objc_cstring_object',
  440. '.objc_sel_fixup',
  441. '__DATA,__objc_data',
  442. '__DATA,__objc_const',
  443. '.objc_superrefs',
  444. '__DATA, __datacoal_nt,coalesced',
  445. '.objc_classlist',
  446. '.objc_nlclasslist',
  447. '.objc_catlist',
  448. '.obcj_nlcatlist',
  449. '.objc_protolist'
  450. );
  451. begin
  452. AsmLn;
  453. AsmWrite('SECTION ');
  454. AsmWrite(secnames[atype]);
  455. if create_smartlink_sections and
  456. (atype<>sec_bss) and
  457. (aname<>'') then
  458. begin
  459. AsmWrite('.');
  460. AsmWrite(aname);
  461. end;
  462. AsmLn;
  463. LasTSecType:=atype;
  464. end;
  465. procedure T386NasmAssembler.WriteTree(p:TAsmList);
  466. {$ifdef cpuextended}
  467. type
  468. t80bitarray = array[0..9] of byte;
  469. {$endif cpuextended}
  470. var
  471. s : string;
  472. hp : tai;
  473. hp1 : tailineinfo;
  474. counter,
  475. lines,
  476. i,j,l : longint;
  477. InlineLevel : longint;
  478. consttype : taiconst_type;
  479. do_line,
  480. quoted : boolean;
  481. co : comp;
  482. sin : single;
  483. d : double;
  484. {$ifdef cpuextended}
  485. e : extended;
  486. {$endif cpuextended}
  487. begin
  488. if not assigned(p) then
  489. exit;
  490. InlineLevel:=0;
  491. { lineinfo is only needed for al_procedures (PFV) }
  492. do_line:=(cs_asm_source in current_settings.globalswitches) or
  493. ((cs_lineinfo in current_settings.moduleswitches)
  494. and (p=current_asmdata.asmlists[al_procedures]));
  495. hp:=tai(p.first);
  496. while assigned(hp) do
  497. begin
  498. if not(hp.typ in SkipLineInfo) then
  499. begin
  500. hp1:=hp as tailineinfo;
  501. current_filepos:=hp1.fileinfo;
  502. if do_line then
  503. begin
  504. { load infile }
  505. if lastfileinfo.fileindex<>hp1.fileinfo.fileindex then
  506. begin
  507. infile:=current_module.sourcefiles.get_file(hp1.fileinfo.fileindex);
  508. if assigned(infile) then
  509. begin
  510. { open only if needed !! }
  511. if (cs_asm_source in current_settings.globalswitches) then
  512. infile.open;
  513. end;
  514. { avoid unnecessary reopens of the same file !! }
  515. lastfileinfo.fileindex:=hp1.fileinfo.fileindex;
  516. { be sure to change line !! }
  517. lastfileinfo.line:=-1;
  518. end;
  519. { write source }
  520. if (cs_asm_source in current_settings.globalswitches) and
  521. assigned(infile) then
  522. begin
  523. if (infile<>lastinfile) then
  524. begin
  525. AsmWriteLn(target_asm.comment+'['+infile.name^+']');
  526. if assigned(lastinfile) then
  527. lastinfile.close;
  528. end;
  529. if (hp1.fileinfo.line<>lastfileinfo.line) and
  530. ((hp1.fileinfo.line<infile.maxlinebuf) or (InlineLevel>0)) then
  531. begin
  532. if (hp1.fileinfo.line<>0) and
  533. ((infile.linebuf^[hp1.fileinfo.line]>=0) or (InlineLevel>0)) then
  534. AsmWriteLn(target_asm.comment+'['+tostr(hp1.fileinfo.line)+'] '+
  535. fixline(infile.GetLineStr(hp1.fileinfo.line)));
  536. { set it to a negative value !
  537. to make that is has been read already !! PM }
  538. if (infile.linebuf^[hp1.fileinfo.line]>=0) then
  539. infile.linebuf^[hp1.fileinfo.line]:=-infile.linebuf^[hp1.fileinfo.line]-1;
  540. end;
  541. end;
  542. lastfileinfo:=hp1.fileinfo;
  543. lastinfile:=infile;
  544. end;
  545. end;
  546. case hp.typ of
  547. ait_comment :
  548. Begin
  549. AsmWrite(target_asm.comment);
  550. AsmWritePChar(tai_comment(hp).str);
  551. AsmLn;
  552. End;
  553. ait_regalloc :
  554. begin
  555. if (cs_asm_regalloc in current_settings.globalswitches) then
  556. AsmWriteLn(#9#9+target_asm.comment+'Register '+nasm_regname(tai_regalloc(hp).reg)+
  557. regallocstr[tai_regalloc(hp).ratype]);
  558. end;
  559. ait_tempalloc :
  560. begin
  561. if (cs_asm_tempalloc in current_settings.globalswitches) then
  562. begin
  563. {$ifdef EXTDEBUG}
  564. if assigned(tai_tempalloc(hp).problem) then
  565. AsmWriteLn(target_asm.comment+tai_tempalloc(hp).problem^+' ('+tostr(tai_tempalloc(hp).temppos)+','+
  566. tostr(tai_tempalloc(hp).tempsize)+')')
  567. else
  568. {$endif EXTDEBUG}
  569. AsmWriteLn(target_asm.comment+'Temp '+tostr(tai_tempalloc(hp).temppos)+','+
  570. tostr(tai_tempalloc(hp).tempsize)+tempallocstr[tai_tempalloc(hp).allocation]);
  571. end;
  572. end;
  573. ait_section :
  574. begin
  575. if tai_section(hp).sectype<>sec_none then
  576. WriteSection(tai_section(hp).sectype,tai_section(hp).name^);
  577. LasTSecType:=tai_section(hp).sectype;
  578. end;
  579. ait_align :
  580. begin
  581. { nasm gives warnings when it finds align in bss as it
  582. wants to store data }
  583. if (lastsectype<>sec_bss) and
  584. (tai_align(hp).aligntype>1) then
  585. AsmWriteLn(#9'ALIGN '+tostr(tai_align(hp).aligntype));
  586. end;
  587. ait_datablock :
  588. begin
  589. if tai_datablock(hp).is_global then
  590. begin
  591. AsmWrite(#9'GLOBAL ');
  592. AsmWriteLn(tai_datablock(hp).sym.name);
  593. end;
  594. AsmWrite(PadTabs(tai_datablock(hp).sym.name,':'));
  595. if SmartAsm then
  596. AddSymbol(tai_datablock(hp).sym.name,true);
  597. AsmWriteLn('RESB'#9+tostr(tai_datablock(hp).size));
  598. end;
  599. ait_const:
  600. begin
  601. consttype:=tai_const(hp).consttype;
  602. case consttype of
  603. aitconst_64bit :
  604. begin
  605. if assigned(tai_const(hp).sym) then
  606. internalerror(200404292);
  607. AsmWrite(ait_const2str[aitconst_32bit]);
  608. AsmWrite(tostr(longint(lo(tai_const(hp).value))));
  609. AsmWrite(',');
  610. AsmWrite(tostr(longint(hi(tai_const(hp).value))));
  611. AsmLn;
  612. end;
  613. aitconst_uleb128bit,
  614. aitconst_sleb128bit,
  615. aitconst_128bit:
  616. begin
  617. end;
  618. aitconst_32bit,
  619. aitconst_16bit,
  620. aitconst_8bit,
  621. aitconst_rva_symbol,
  622. aitconst_secrel32_symbol,
  623. aitconst_indirect_symbol :
  624. begin
  625. AsmWrite(ait_const2str[tai_const(hp).consttype]);
  626. l:=0;
  627. repeat
  628. if assigned(tai_const(hp).sym) then
  629. begin
  630. if SmartAsm then
  631. begin
  632. AddSymbol(tai_const(hp).sym.name,false);
  633. if assigned(tai_const(hp).endsym) then
  634. AddSymbol(tai_const(hp).endsym.name,false);
  635. end;
  636. if assigned(tai_const(hp).endsym) then
  637. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  638. else
  639. s:=tai_const(hp).sym.name;
  640. if tai_const(hp).value<>0 then
  641. s:=s+tostr_with_plus(tai_const(hp).value);
  642. end
  643. else
  644. s:=tostr(tai_const(hp).value);
  645. AsmWrite(s);
  646. inc(l,length(s));
  647. if (l>line_length) or
  648. (hp.next=nil) or
  649. (tai(hp.next).typ<>ait_const) or
  650. (tai_const(hp.next).consttype<>consttype) then
  651. break;
  652. hp:=tai(hp.next);
  653. AsmWrite(',');
  654. until false;
  655. AsmLn;
  656. end;
  657. else
  658. internalerror(200704252);
  659. end;
  660. end;
  661. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  662. ait_real_80bit :
  663. begin
  664. if do_line then
  665. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  666. { Make sure e is a extended type, bestreal could be
  667. a different type (bestreal) !! (PFV) }
  668. e:=tai_real_80bit(hp).value;
  669. AsmWrite(#9#9'DB'#9);
  670. for i:=0 to 9 do
  671. begin
  672. if i<>0 then
  673. AsmWrite(',');
  674. AsmWrite(tostr(t80bitarray(e)[i]));
  675. end;
  676. AsmLn;
  677. end;
  678. {$else cpuextended}
  679. ait_real_80bit :
  680. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  681. {$endif cpuextended}
  682. // ait_real_64bit :
  683. // AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  684. ait_real_64bit :
  685. begin
  686. if do_line then
  687. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  688. d:=tai_real_64bit(hp).value;
  689. { swap the values to correct endian if required }
  690. if source_info.endian <> target_info.endian then
  691. swap64bitarray(t64bitarray(d));
  692. AsmWrite(#9#9'DB'#9);
  693. {$ifdef arm}
  694. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  695. begin
  696. for i:=4 to 7 do
  697. begin
  698. if i<>4 then
  699. AsmWrite(',');
  700. AsmWrite(tostr(t64bitarray(d)[i]));
  701. end;
  702. for i:=0 to 3 do
  703. begin
  704. AsmWrite(',');
  705. AsmWrite(tostr(t64bitarray(d)[i]));
  706. end;
  707. end
  708. else
  709. {$endif arm}
  710. begin
  711. for i:=0 to 7 do
  712. begin
  713. if i<>0 then
  714. AsmWrite(',');
  715. AsmWrite(tostr(t64bitarray(d)[i]));
  716. end;
  717. end;
  718. AsmLn;
  719. end;
  720. // ait_real_32bit :
  721. // AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  722. ait_real_32bit :
  723. begin
  724. if do_line then
  725. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  726. sin:=tai_real_32bit(hp).value;
  727. { swap the values to correct endian if required }
  728. if source_info.endian <> target_info.endian then
  729. swap32bitarray(t32bitarray(sin));
  730. AsmWrite(#9#9'DB'#9);
  731. for i:=0 to 3 do
  732. begin
  733. if i<>0 then
  734. AsmWrite(',');
  735. AsmWrite(tostr(t32bitarray(sin)[i]));
  736. end;
  737. AsmLn;
  738. end;
  739. // ait_comp_64bit :
  740. // AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  741. ait_comp_64bit :
  742. begin
  743. if do_line then
  744. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  745. AsmWrite(#9#9'DB'#9);
  746. co:=comp(tai_comp_64bit(hp).value);
  747. { swap the values to correct endian if required }
  748. if source_info.endian <> target_info.endian then
  749. swap64bitarray(t64bitarray(co));
  750. for i:=0 to 7 do
  751. begin
  752. if i<>0 then
  753. AsmWrite(',');
  754. AsmWrite(tostr(t64bitarray(co)[i]));
  755. end;
  756. AsmLn;
  757. end;
  758. ait_string :
  759. begin
  760. counter := 0;
  761. lines := tai_string(hp).len div line_length;
  762. { separate lines in different parts }
  763. if tai_string(hp).len > 0 then
  764. Begin
  765. for j := 0 to lines-1 do
  766. begin
  767. AsmWrite(#9#9'DB'#9);
  768. quoted:=false;
  769. for i:=counter to counter+line_length-1 do
  770. begin
  771. { it is an ascii character. }
  772. if (ord(tai_string(hp).str[i])>31) and
  773. (ord(tai_string(hp).str[i])<128) and
  774. (tai_string(hp).str[i]<>'"') then
  775. begin
  776. if not(quoted) then
  777. begin
  778. if i>counter then
  779. AsmWrite(',');
  780. AsmWrite('"');
  781. end;
  782. AsmWrite(tai_string(hp).str[i]);
  783. quoted:=true;
  784. end { if > 31 and < 128 and ord('"') }
  785. else
  786. begin
  787. if quoted then
  788. AsmWrite('"');
  789. if i>counter then
  790. AsmWrite(',');
  791. quoted:=false;
  792. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  793. end;
  794. end; { end for i:=0 to... }
  795. if quoted then AsmWrite('"');
  796. AsmWrite(target_info.newline);
  797. inc(counter,line_length);
  798. end; { end for j:=0 ... }
  799. { do last line of lines }
  800. if counter<tai_string(hp).len then
  801. AsmWrite(#9#9'DB'#9);
  802. quoted:=false;
  803. for i:=counter to tai_string(hp).len-1 do
  804. begin
  805. { it is an ascii character. }
  806. if (ord(tai_string(hp).str[i])>31) and
  807. (ord(tai_string(hp).str[i])<128) and
  808. (tai_string(hp).str[i]<>'"') then
  809. begin
  810. if not(quoted) then
  811. begin
  812. if i>counter then
  813. AsmWrite(',');
  814. AsmWrite('"');
  815. end;
  816. AsmWrite(tai_string(hp).str[i]);
  817. quoted:=true;
  818. end { if > 31 and < 128 and " }
  819. else
  820. begin
  821. if quoted then
  822. AsmWrite('"');
  823. if i>counter then
  824. AsmWrite(',');
  825. quoted:=false;
  826. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  827. end;
  828. end; { end for i:=0 to... }
  829. if quoted then
  830. AsmWrite('"');
  831. end;
  832. AsmLn;
  833. end;
  834. ait_label :
  835. begin
  836. if tai_label(hp).labsym.is_used then
  837. AsmWriteLn(tai_label(hp).labsym.name+':');
  838. if SmartAsm then
  839. AddSymbol(tai_label(hp).labsym.name,true);
  840. end;
  841. ait_symbol :
  842. begin
  843. if tai_symbol(hp).has_value then
  844. internalerror(2009090803);
  845. if tai_symbol(hp).is_global then
  846. begin
  847. AsmWrite(#9'GLOBAL ');
  848. AsmWriteLn(tai_symbol(hp).sym.name);
  849. end;
  850. AsmWrite(tai_symbol(hp).sym.name);
  851. if SmartAsm then
  852. AddSymbol(tai_symbol(hp).sym.name,true);
  853. if assigned(hp.next) and not(tai(hp.next).typ in
  854. [ait_const,
  855. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  856. AsmWriteLn(':')
  857. end;
  858. ait_symbol_end : ;
  859. ait_instruction :
  860. begin
  861. taicpu(hp).CheckNonCommutativeOpcodes;
  862. { We need intel order, no At&t }
  863. taicpu(hp).SetOperandOrder(op_intel);
  864. s:='';
  865. if ((taicpu(hp).opcode=A_FADDP) or
  866. (taicpu(hp).opcode=A_FMULP))
  867. and (taicpu(hp).ops=0) then
  868. begin
  869. taicpu(hp).allocate_oper(2);
  870. taicpu(hp).oper[0]^.typ:=top_reg;
  871. taicpu(hp).oper[0]^.reg:=NR_ST1;
  872. taicpu(hp).oper[1]^.typ:=top_reg;
  873. taicpu(hp).oper[1]^.reg:=NR_ST;
  874. end;
  875. if taicpu(hp).opcode=A_FWAIT then
  876. AsmWriteln(#9#9'DB'#9'09bh')
  877. else
  878. begin
  879. { We need to explicitely set
  880. word prefix to get selectors
  881. to be pushed in 2 bytes PM }
  882. if (taicpu(hp).opsize=S_W) and
  883. ((taicpu(hp).opcode=A_PUSH) or
  884. (taicpu(hp).opcode=A_POP)) and
  885. (taicpu(hp).oper[0]^.typ=top_reg) and
  886. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  887. AsmWriteln(#9#9'DB'#9'066h');
  888. AsmWrite(#9#9+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]);
  889. if taicpu(hp).ops<>0 then
  890. begin
  891. if is_calljmp(taicpu(hp).opcode) then
  892. begin
  893. AsmWrite(#9);
  894. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opcode);
  895. end
  896. else
  897. begin
  898. for i:=0 to taicpu(hp).ops-1 do
  899. begin
  900. if i=0 then
  901. AsmWrite(#9)
  902. else
  903. AsmWrite(',');
  904. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,taicpu(hp).ops,(i=2));
  905. end;
  906. end;
  907. end;
  908. AsmLn;
  909. end;
  910. end;
  911. ait_stab,
  912. ait_force_line,
  913. ait_function_name : ;
  914. ait_cutobject :
  915. begin
  916. if SmartAsm then
  917. begin
  918. { only reset buffer if nothing has changed }
  919. if AsmSize=AsmStartSize then
  920. AsmClear
  921. else
  922. begin
  923. if SmartAsm then
  924. begin
  925. WriteSmartExternals;
  926. FreeExternChainList;
  927. end;
  928. AsmClose;
  929. DoAssemble;
  930. AsmCreate(tai_cutobject(hp).place);
  931. end;
  932. { avoid empty files }
  933. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  934. begin
  935. if tai(hp.next).typ=ait_section then
  936. lasTSectype:=tai_section(hp.next).sectype;
  937. hp:=tai(hp.next);
  938. end;
  939. if lasTSectype<>sec_none then
  940. WriteSection(lasTSectype,'');
  941. AsmStartSize:=AsmSize;
  942. end;
  943. end;
  944. ait_marker :
  945. if tai_marker(hp).kind=mark_InlineStart then
  946. inc(InlineLevel)
  947. else if tai_marker(hp).kind=mark_InlineEnd then
  948. dec(InlineLevel);
  949. ait_directive :
  950. begin
  951. case tai_directive(hp).directive of
  952. asd_nasm_import :
  953. AsmWrite('import ');
  954. asd_extern :
  955. AsmWrite('EXTERN ');
  956. else
  957. internalerror(200509191);
  958. end;
  959. if assigned(tai_directive(hp).name) then
  960. begin
  961. if SmartAsm then
  962. AddSymbol(tai_directive(hp).name^,false);
  963. AsmWrite(tai_directive(hp).name^);
  964. end;
  965. AsmLn;
  966. end;
  967. else
  968. internalerror(10000);
  969. end;
  970. hp:=tai(hp.next);
  971. end;
  972. end;
  973. procedure T386NasmAssembler.WriteExternals;
  974. var
  975. sym : TAsmSymbol;
  976. i : longint;
  977. begin
  978. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  979. begin
  980. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  981. if sym.bind=AB_EXTERNAL then
  982. AsmWriteln('EXTERN'#9+sym.name);
  983. end;
  984. end;
  985. procedure T386NasmAssembler.WriteSmartExternals;
  986. var
  987. EC : PExternChain;
  988. begin
  989. EC:=FEC;
  990. while assigned(EC) do
  991. begin
  992. if not EC^.is_defined then
  993. AsmWriteln('EXTERN'#9+EC^.psym^);
  994. EC:=EC^.next;
  995. end;
  996. end;
  997. procedure T386NasmAssembler.WriteAsmList;
  998. var
  999. hal : tasmlisttype;
  1000. begin
  1001. {$ifdef EXTDEBUG}
  1002. if assigned(current_module.mainsource) then
  1003. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource^);
  1004. {$endif}
  1005. AsmWriteLn('BITS 32');
  1006. AsmLn;
  1007. WriteExternals;
  1008. for hal:=low(TasmlistType) to high(TasmlistType) do
  1009. begin
  1010. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  1011. writetree(current_asmdata.asmlists[hal]);
  1012. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  1013. end;
  1014. AsmLn;
  1015. if SmartAsm then
  1016. begin
  1017. WriteSmartExternals;
  1018. FreeExternChainList;
  1019. end;
  1020. {$ifdef EXTDEBUG}
  1021. if assigned(current_module.mainsource) then
  1022. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource^);
  1023. {$endif EXTDEBUG}
  1024. end;
  1025. {*****************************************************************************
  1026. Initialize
  1027. *****************************************************************************}
  1028. const
  1029. as_i386_nasmcoff_info : tasminfo =
  1030. (
  1031. id : as_i386_nasmcoff;
  1032. idtxt : 'NASMCOFF';
  1033. asmbin : 'nasm';
  1034. asmcmd : '-f coff -o $OBJ $ASM';
  1035. supported_targets : [system_i386_go32v2];
  1036. flags : [af_allowdirect,af_needar,af_no_debug];
  1037. labelprefix : '..@';
  1038. comment : '; ';
  1039. );
  1040. as_i386_nasmwin32_info : tasminfo =
  1041. (
  1042. id : as_i386_nasmwin32;
  1043. idtxt : 'NASMWIN32';
  1044. asmbin : 'nasm';
  1045. asmcmd : '-f win32 -o $OBJ $ASM';
  1046. supported_targets : [system_i386_win32];
  1047. flags : [af_allowdirect,af_needar,af_no_debug];
  1048. labelprefix : '..@';
  1049. comment : '; ';
  1050. );
  1051. as_i386_nasmobj_info : tasminfo =
  1052. (
  1053. id : as_i386_nasmobj;
  1054. idtxt : 'NASMOBJ';
  1055. asmbin : 'nasm';
  1056. asmcmd : '-f obj -o $OBJ $ASM';
  1057. supported_targets : [system_i386_embedded];
  1058. flags : [af_allowdirect,af_needar,af_no_debug];
  1059. labelprefix : '..@';
  1060. comment : '; ';
  1061. );
  1062. as_i386_nasmwdosx_info : tasminfo =
  1063. (
  1064. id : as_i386_nasmwdosx;
  1065. idtxt : 'NASMWDOSX';
  1066. asmbin : 'nasm';
  1067. asmcmd : '-f win32 -o $OBJ $ASM';
  1068. supported_targets : [system_i386_wdosx];
  1069. flags : [af_allowdirect,af_needar,af_no_debug];
  1070. labelprefix : '..@';
  1071. comment : '; ';
  1072. );
  1073. as_i386_nasmelf_info : tasminfo =
  1074. (
  1075. id : as_i386_nasmelf;
  1076. idtxt : 'NASMELF';
  1077. asmbin : 'nasm';
  1078. asmcmd : '-f elf -o $OBJ $ASM';
  1079. supported_targets : [system_i386_linux];
  1080. flags : [af_allowdirect,af_needar,af_no_debug];
  1081. labelprefix : '..@';
  1082. comment : '; ';
  1083. );
  1084. as_i386_nasmbeos_info : tasminfo =
  1085. (
  1086. id : as_i386_nasmbeos;
  1087. idtxt : 'NASMELF';
  1088. asmbin : 'nasm';
  1089. asmcmd : '-f elf -o $OBJ $ASM';
  1090. supported_targets : [system_i386_beos];
  1091. flags : [af_allowdirect,af_needar,af_no_debug];
  1092. labelprefix : '..@';
  1093. comment : '; ';
  1094. );
  1095. as_i386_nasmhaiku_info : tasminfo =
  1096. (
  1097. id : as_i386_nasmhaiku;
  1098. idtxt : 'NASMELF';
  1099. asmbin : 'nasm';
  1100. asmcmd : '-f elf -o $OBJ $ASM';
  1101. supported_targets : [system_i386_haiku];
  1102. flags : [af_allowdirect,af_needar,af_no_debug];
  1103. labelprefix : '..@';
  1104. comment : '; ';
  1105. );
  1106. initialization
  1107. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  1108. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  1109. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  1110. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  1111. RegisterAssembler(as_i386_nasmbeos_info,T386NasmAssembler);
  1112. RegisterAssembler(as_i386_nasmhaiku_info,T386NasmAssembler);
  1113. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  1114. end.