ag386nsm.pas 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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_secrel32_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
  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','.data','.data','.data','.data',
  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. begin
  624. AsmWrite(ait_const2str[tai_const(hp).consttype]);
  625. l:=0;
  626. repeat
  627. if assigned(tai_const(hp).sym) then
  628. begin
  629. if SmartAsm then
  630. begin
  631. AddSymbol(tai_const(hp).sym.name,false);
  632. if assigned(tai_const(hp).endsym) then
  633. AddSymbol(tai_const(hp).endsym.name,false);
  634. end;
  635. if assigned(tai_const(hp).endsym) then
  636. s:=tai_const(hp).endsym.name+'-'+tai_const(hp).sym.name
  637. else
  638. s:=tai_const(hp).sym.name;
  639. if tai_const(hp).value<>0 then
  640. s:=s+tostr_with_plus(tai_const(hp).value);
  641. end
  642. else
  643. s:=tostr(tai_const(hp).value);
  644. AsmWrite(s);
  645. inc(l,length(s));
  646. if (l>line_length) or
  647. (hp.next=nil) or
  648. (tai(hp.next).typ<>ait_const) or
  649. (tai_const(hp.next).consttype<>consttype) then
  650. break;
  651. hp:=tai(hp.next);
  652. AsmWrite(',');
  653. until false;
  654. AsmLn;
  655. end;
  656. else
  657. internalerror(200704252);
  658. end;
  659. end;
  660. {$if defined(cpuextended) and defined(FPC_HAS_TYPE_EXTENDED)}
  661. ait_real_80bit :
  662. begin
  663. if do_line then
  664. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_real_80bit(hp).value));
  665. { Make sure e is a extended type, bestreal could be
  666. a different type (bestreal) !! (PFV) }
  667. e:=tai_real_80bit(hp).value;
  668. AsmWrite(#9#9'DB'#9);
  669. for i:=0 to 9 do
  670. begin
  671. if i<>0 then
  672. AsmWrite(',');
  673. AsmWrite(tostr(t80bitarray(e)[i]));
  674. end;
  675. AsmLn;
  676. end;
  677. {$else cpuextended}
  678. ait_real_80bit :
  679. AsmWriteLn(#9#9'DT'#9+extended2str(tai_real_80bit(hp).value));
  680. {$endif cpuextended}
  681. // ait_real_64bit :
  682. // AsmWriteLn(#9#9'DQ'#9+double2str(tai_real_64bit(hp).value));
  683. ait_real_64bit :
  684. begin
  685. if do_line then
  686. AsmWriteLn(target_asm.comment+'value: '+double2str(tai_real_64bit(hp).value));
  687. d:=tai_real_64bit(hp).value;
  688. { swap the values to correct endian if required }
  689. if source_info.endian <> target_info.endian then
  690. swap64bitarray(t64bitarray(d));
  691. AsmWrite(#9#9'DB'#9);
  692. {$ifdef arm}
  693. if tai_real_64bit(hp).formatoptions=fo_hiloswapped then
  694. begin
  695. for i:=4 to 7 do
  696. begin
  697. if i<>4 then
  698. AsmWrite(',');
  699. AsmWrite(tostr(t64bitarray(d)[i]));
  700. end;
  701. for i:=0 to 3 do
  702. begin
  703. AsmWrite(',');
  704. AsmWrite(tostr(t64bitarray(d)[i]));
  705. end;
  706. end
  707. else
  708. {$endif arm}
  709. begin
  710. for i:=0 to 7 do
  711. begin
  712. if i<>0 then
  713. AsmWrite(',');
  714. AsmWrite(tostr(t64bitarray(d)[i]));
  715. end;
  716. end;
  717. AsmLn;
  718. end;
  719. // ait_real_32bit :
  720. // AsmWriteLn(#9#9'DD'#9+single2str(tai_real_32bit(hp).value));
  721. ait_real_32bit :
  722. begin
  723. if do_line then
  724. AsmWriteLn(target_asm.comment+'value: '+single2str(tai_real_32bit(hp).value));
  725. sin:=tai_real_32bit(hp).value;
  726. { swap the values to correct endian if required }
  727. if source_info.endian <> target_info.endian then
  728. swap32bitarray(t32bitarray(sin));
  729. AsmWrite(#9#9'DB'#9);
  730. for i:=0 to 3 do
  731. begin
  732. if i<>0 then
  733. AsmWrite(',');
  734. AsmWrite(tostr(t32bitarray(sin)[i]));
  735. end;
  736. AsmLn;
  737. end;
  738. // ait_comp_64bit :
  739. // AsmWriteLn(#9#9'DQ'#9+comp2str(tai_real_80bit(hp).value));
  740. ait_comp_64bit :
  741. begin
  742. if do_line then
  743. AsmWriteLn(target_asm.comment+'value: '+extended2str(tai_comp_64bit(hp).value));
  744. AsmWrite(#9#9'DB'#9);
  745. co:=comp(tai_comp_64bit(hp).value);
  746. { swap the values to correct endian if required }
  747. if source_info.endian <> target_info.endian then
  748. swap64bitarray(t64bitarray(co));
  749. for i:=0 to 7 do
  750. begin
  751. if i<>0 then
  752. AsmWrite(',');
  753. AsmWrite(tostr(t64bitarray(co)[i]));
  754. end;
  755. AsmLn;
  756. end;
  757. ait_string :
  758. begin
  759. counter := 0;
  760. lines := tai_string(hp).len div line_length;
  761. { separate lines in different parts }
  762. if tai_string(hp).len > 0 then
  763. Begin
  764. for j := 0 to lines-1 do
  765. begin
  766. AsmWrite(#9#9'DB'#9);
  767. quoted:=false;
  768. for i:=counter to counter+line_length-1 do
  769. begin
  770. { it is an ascii character. }
  771. if (ord(tai_string(hp).str[i])>31) and
  772. (ord(tai_string(hp).str[i])<128) and
  773. (tai_string(hp).str[i]<>'"') then
  774. begin
  775. if not(quoted) then
  776. begin
  777. if i>counter then
  778. AsmWrite(',');
  779. AsmWrite('"');
  780. end;
  781. AsmWrite(tai_string(hp).str[i]);
  782. quoted:=true;
  783. end { if > 31 and < 128 and ord('"') }
  784. else
  785. begin
  786. if quoted then
  787. AsmWrite('"');
  788. if i>counter then
  789. AsmWrite(',');
  790. quoted:=false;
  791. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  792. end;
  793. end; { end for i:=0 to... }
  794. if quoted then AsmWrite('"');
  795. AsmWrite(target_info.newline);
  796. inc(counter,line_length);
  797. end; { end for j:=0 ... }
  798. { do last line of lines }
  799. if counter<tai_string(hp).len then
  800. AsmWrite(#9#9'DB'#9);
  801. quoted:=false;
  802. for i:=counter to tai_string(hp).len-1 do
  803. begin
  804. { it is an ascii character. }
  805. if (ord(tai_string(hp).str[i])>31) and
  806. (ord(tai_string(hp).str[i])<128) and
  807. (tai_string(hp).str[i]<>'"') then
  808. begin
  809. if not(quoted) then
  810. begin
  811. if i>counter then
  812. AsmWrite(',');
  813. AsmWrite('"');
  814. end;
  815. AsmWrite(tai_string(hp).str[i]);
  816. quoted:=true;
  817. end { if > 31 and < 128 and " }
  818. else
  819. begin
  820. if quoted then
  821. AsmWrite('"');
  822. if i>counter then
  823. AsmWrite(',');
  824. quoted:=false;
  825. AsmWrite(tostr(ord(tai_string(hp).str[i])));
  826. end;
  827. end; { end for i:=0 to... }
  828. if quoted then
  829. AsmWrite('"');
  830. end;
  831. AsmLn;
  832. end;
  833. ait_label :
  834. begin
  835. if tai_label(hp).labsym.is_used then
  836. AsmWriteLn(tai_label(hp).labsym.name+':');
  837. if SmartAsm then
  838. AddSymbol(tai_label(hp).labsym.name,true);
  839. end;
  840. ait_symbol :
  841. begin
  842. if tai_symbol(hp).has_value then
  843. internalerror(2009090803);
  844. if tai_symbol(hp).is_global then
  845. begin
  846. AsmWrite(#9'GLOBAL ');
  847. AsmWriteLn(tai_symbol(hp).sym.name);
  848. end;
  849. AsmWrite(tai_symbol(hp).sym.name);
  850. if SmartAsm then
  851. AddSymbol(tai_symbol(hp).sym.name,true);
  852. if assigned(hp.next) and not(tai(hp.next).typ in
  853. [ait_const,
  854. ait_real_32bit,ait_real_64bit,ait_real_80bit,ait_comp_64bit,ait_string]) then
  855. AsmWriteLn(':')
  856. end;
  857. ait_symbol_end : ;
  858. ait_instruction :
  859. begin
  860. taicpu(hp).CheckNonCommutativeOpcodes;
  861. { We need intel order, no At&t }
  862. taicpu(hp).SetOperandOrder(op_intel);
  863. s:='';
  864. if ((taicpu(hp).opcode=A_FADDP) or
  865. (taicpu(hp).opcode=A_FMULP))
  866. and (taicpu(hp).ops=0) then
  867. begin
  868. taicpu(hp).allocate_oper(2);
  869. taicpu(hp).oper[0]^.typ:=top_reg;
  870. taicpu(hp).oper[0]^.reg:=NR_ST1;
  871. taicpu(hp).oper[1]^.typ:=top_reg;
  872. taicpu(hp).oper[1]^.reg:=NR_ST;
  873. end;
  874. if taicpu(hp).opcode=A_FWAIT then
  875. AsmWriteln(#9#9'DB'#9'09bh')
  876. else
  877. begin
  878. { We need to explicitely set
  879. word prefix to get selectors
  880. to be pushed in 2 bytes PM }
  881. if (taicpu(hp).opsize=S_W) and
  882. ((taicpu(hp).opcode=A_PUSH) or
  883. (taicpu(hp).opcode=A_POP)) and
  884. (taicpu(hp).oper[0]^.typ=top_reg) and
  885. (is_segment_reg(taicpu(hp).oper[0]^.reg)) then
  886. AsmWriteln(#9#9'DB'#9'066h');
  887. AsmWrite(#9#9+std_op2str[taicpu(hp).opcode]+cond2str[taicpu(hp).condition]);
  888. if taicpu(hp).ops<>0 then
  889. begin
  890. if is_calljmp(taicpu(hp).opcode) then
  891. begin
  892. AsmWrite(#9);
  893. WriteOper_jmp(taicpu(hp).oper[0]^,taicpu(hp).opcode);
  894. end
  895. else
  896. begin
  897. for i:=0 to taicpu(hp).ops-1 do
  898. begin
  899. if i=0 then
  900. AsmWrite(#9)
  901. else
  902. AsmWrite(',');
  903. WriteOper(taicpu(hp).oper[i]^,taicpu(hp).opsize,taicpu(hp).opcode,taicpu(hp).ops,(i=2));
  904. end;
  905. end;
  906. end;
  907. AsmLn;
  908. end;
  909. end;
  910. ait_stab,
  911. ait_force_line,
  912. ait_function_name : ;
  913. ait_cutobject :
  914. begin
  915. if SmartAsm then
  916. begin
  917. { only reset buffer if nothing has changed }
  918. if AsmSize=AsmStartSize then
  919. AsmClear
  920. else
  921. begin
  922. if SmartAsm then
  923. begin
  924. WriteSmartExternals;
  925. FreeExternChainList;
  926. end;
  927. AsmClose;
  928. DoAssemble;
  929. AsmCreate(tai_cutobject(hp).place);
  930. end;
  931. { avoid empty files }
  932. while assigned(hp.next) and (tai(hp.next).typ in [ait_cutobject,ait_section,ait_comment]) do
  933. begin
  934. if tai(hp.next).typ=ait_section then
  935. lasTSectype:=tai_section(hp.next).sectype;
  936. hp:=tai(hp.next);
  937. end;
  938. if lasTSectype<>sec_none then
  939. WriteSection(lasTSectype,'');
  940. AsmStartSize:=AsmSize;
  941. end;
  942. end;
  943. ait_marker :
  944. if tai_marker(hp).kind=mark_InlineStart then
  945. inc(InlineLevel)
  946. else if tai_marker(hp).kind=mark_InlineEnd then
  947. dec(InlineLevel);
  948. ait_directive :
  949. begin
  950. case tai_directive(hp).directive of
  951. asd_nasm_import :
  952. AsmWrite('import ');
  953. asd_extern :
  954. AsmWrite('EXTERN ');
  955. else
  956. internalerror(200509191);
  957. end;
  958. if assigned(tai_directive(hp).name) then
  959. begin
  960. if SmartAsm then
  961. AddSymbol(tai_directive(hp).name^,false);
  962. AsmWrite(tai_directive(hp).name^);
  963. end;
  964. AsmLn;
  965. end;
  966. else
  967. internalerror(10000);
  968. end;
  969. hp:=tai(hp.next);
  970. end;
  971. end;
  972. procedure T386NasmAssembler.WriteExternals;
  973. var
  974. sym : TAsmSymbol;
  975. i : longint;
  976. begin
  977. for i:=0 to current_asmdata.AsmSymbolDict.Count-1 do
  978. begin
  979. sym:=TAsmSymbol(current_asmdata.AsmSymbolDict[i]);
  980. if sym.bind=AB_EXTERNAL then
  981. AsmWriteln('EXTERN'#9+sym.name);
  982. end;
  983. end;
  984. procedure T386NasmAssembler.WriteSmartExternals;
  985. var
  986. EC : PExternChain;
  987. begin
  988. EC:=FEC;
  989. while assigned(EC) do
  990. begin
  991. if not EC^.is_defined then
  992. AsmWriteln('EXTERN'#9+EC^.psym^);
  993. EC:=EC^.next;
  994. end;
  995. end;
  996. procedure T386NasmAssembler.WriteAsmList;
  997. var
  998. hal : tasmlisttype;
  999. begin
  1000. {$ifdef EXTDEBUG}
  1001. if assigned(current_module.mainsource) then
  1002. comment(v_info,'Start writing nasm-styled assembler output for '+current_module.mainsource^);
  1003. {$endif}
  1004. AsmWriteLn('BITS 32');
  1005. AsmLn;
  1006. WriteExternals;
  1007. for hal:=low(TasmlistType) to high(TasmlistType) do
  1008. begin
  1009. AsmWriteLn(target_asm.comment+'Begin asmlist '+AsmListTypeStr[hal]);
  1010. writetree(current_asmdata.asmlists[hal]);
  1011. AsmWriteLn(target_asm.comment+'End asmlist '+AsmListTypeStr[hal]);
  1012. end;
  1013. AsmLn;
  1014. if SmartAsm then
  1015. begin
  1016. WriteSmartExternals;
  1017. FreeExternChainList;
  1018. end;
  1019. {$ifdef EXTDEBUG}
  1020. if assigned(current_module.mainsource) then
  1021. comment(v_info,'Done writing nasm-styled assembler output for '+current_module.mainsource^);
  1022. {$endif EXTDEBUG}
  1023. end;
  1024. {*****************************************************************************
  1025. Initialize
  1026. *****************************************************************************}
  1027. const
  1028. as_i386_nasmcoff_info : tasminfo =
  1029. (
  1030. id : as_i386_nasmcoff;
  1031. idtxt : 'NASMCOFF';
  1032. asmbin : 'nasm';
  1033. asmcmd : '-f coff -o $OBJ $ASM';
  1034. supported_targets : [system_i386_go32v2];
  1035. flags : [af_allowdirect,af_needar,af_no_debug];
  1036. labelprefix : '..@';
  1037. comment : '; ';
  1038. );
  1039. as_i386_nasmwin32_info : tasminfo =
  1040. (
  1041. id : as_i386_nasmwin32;
  1042. idtxt : 'NASMWIN32';
  1043. asmbin : 'nasm';
  1044. asmcmd : '-f win32 -o $OBJ $ASM';
  1045. supported_targets : [system_i386_win32];
  1046. flags : [af_allowdirect,af_needar,af_no_debug];
  1047. labelprefix : '..@';
  1048. comment : '; ';
  1049. );
  1050. as_i386_nasmobj_info : tasminfo =
  1051. (
  1052. id : as_i386_nasmobj;
  1053. idtxt : 'NASMOBJ';
  1054. asmbin : 'nasm';
  1055. asmcmd : '-f obj -o $OBJ $ASM';
  1056. supported_targets : [system_i386_embedded];
  1057. flags : [af_allowdirect,af_needar,af_no_debug];
  1058. labelprefix : '..@';
  1059. comment : '; ';
  1060. );
  1061. as_i386_nasmwdosx_info : tasminfo =
  1062. (
  1063. id : as_i386_nasmwdosx;
  1064. idtxt : 'NASMWDOSX';
  1065. asmbin : 'nasm';
  1066. asmcmd : '-f win32 -o $OBJ $ASM';
  1067. supported_targets : [system_i386_wdosx];
  1068. flags : [af_allowdirect,af_needar,af_no_debug];
  1069. labelprefix : '..@';
  1070. comment : '; ';
  1071. );
  1072. as_i386_nasmelf_info : tasminfo =
  1073. (
  1074. id : as_i386_nasmelf;
  1075. idtxt : 'NASMELF';
  1076. asmbin : 'nasm';
  1077. asmcmd : '-f elf -o $OBJ $ASM';
  1078. supported_targets : [system_i386_linux];
  1079. flags : [af_allowdirect,af_needar,af_no_debug];
  1080. labelprefix : '..@';
  1081. comment : '; ';
  1082. );
  1083. as_i386_nasmbeos_info : tasminfo =
  1084. (
  1085. id : as_i386_nasmbeos;
  1086. idtxt : 'NASMELF';
  1087. asmbin : 'nasm';
  1088. asmcmd : '-f elf -o $OBJ $ASM';
  1089. supported_targets : [system_i386_beos];
  1090. flags : [af_allowdirect,af_needar,af_no_debug];
  1091. labelprefix : '..@';
  1092. comment : '; ';
  1093. );
  1094. as_i386_nasmhaiku_info : tasminfo =
  1095. (
  1096. id : as_i386_nasmhaiku;
  1097. idtxt : 'NASMELF';
  1098. asmbin : 'nasm';
  1099. asmcmd : '-f elf -o $OBJ $ASM';
  1100. supported_targets : [system_i386_haiku];
  1101. flags : [af_allowdirect,af_needar,af_no_debug];
  1102. labelprefix : '..@';
  1103. comment : '; ';
  1104. );
  1105. initialization
  1106. RegisterAssembler(as_i386_nasmcoff_info,T386NasmAssembler);
  1107. RegisterAssembler(as_i386_nasmwin32_info,T386NasmAssembler);
  1108. RegisterAssembler(as_i386_nasmwdosx_info,T386NasmAssembler);
  1109. RegisterAssembler(as_i386_nasmobj_info,T386NasmAssembler);
  1110. RegisterAssembler(as_i386_nasmbeos_info,T386NasmAssembler);
  1111. RegisterAssembler(as_i386_nasmhaiku_info,T386NasmAssembler);
  1112. RegisterAssembler(as_i386_nasmelf_info,T386NasmAssembler);
  1113. end.