assemble.pas 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481
  1. {
  2. Copyright (c) 1998-2004 by Peter Vreman
  3. This unit handles the assemblerfile write and assembler calls of FPC
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. {# @abstract(This unit handles the assembler file write and assembler calls of FPC)
  18. Handles the calls to the actual external assemblers, as well as the generation
  19. of object files for smart linking. Also contains the base class for writing
  20. the assembler statements to file.
  21. }
  22. unit assemble;
  23. {$i fpcdefs.inc}
  24. interface
  25. uses
  26. {$IFDEF USE_SYSUTILS}
  27. sysutils,
  28. {$ELSE USE_SYSUTILS}
  29. strings,
  30. dos,
  31. {$ENDIF USE_SYSUTILS}
  32. systems,globtype,globals,aasmbase,aasmtai,ogbase;
  33. const
  34. { maximum of aasmoutput lists there will be }
  35. maxoutputlists = 20;
  36. { buffer size for writing the .s file }
  37. AsmOutSize=32768;
  38. type
  39. TAssembler=class(TAbstractAssembler)
  40. public
  41. {filenames}
  42. path : pathstr;
  43. name : namestr;
  44. asmfile, { current .s and .o file }
  45. objfile : string;
  46. ppufilename : string;
  47. asmprefix : string;
  48. SmartAsm : boolean;
  49. SmartFilesCount,
  50. SmartHeaderCount : longint;
  51. Constructor Create(smart:boolean);virtual;
  52. Destructor Destroy;override;
  53. procedure NextSmartName(place:tcutplace);
  54. procedure MakeObject;virtual;abstract;
  55. end;
  56. {# This is the base class which should be overriden for each each
  57. assembler writer. It is used to actually assembler a file,
  58. and write the output to the assembler file.
  59. }
  60. TExternalAssembler=class(TAssembler)
  61. private
  62. procedure CreateSmartLinkPath(const s:string);
  63. protected
  64. {outfile}
  65. AsmSize,
  66. AsmStartSize,
  67. outcnt : longint;
  68. outbuf : array[0..AsmOutSize-1] of char;
  69. outfile : file;
  70. ioerror : boolean;
  71. public
  72. {# Returns the complete path and executable name of the assembler
  73. program.
  74. It first tries looking in the UTIL directory if specified,
  75. otherwise it searches in the free pascal binary directory, in
  76. the current working directory and then in the directories
  77. in the $PATH environment.}
  78. Function FindAssembler:string;
  79. {# Actually does the call to the assembler file. Returns false
  80. if the assembling of the file failed.}
  81. Function CallAssembler(const command:string; const para:TCmdStr):Boolean;
  82. Function DoAssemble:boolean;virtual;
  83. Procedure RemoveAsm;
  84. Procedure AsmFlush;
  85. Procedure AsmClear;
  86. {# Write a string to the assembler file }
  87. Procedure AsmWrite(const s:string);
  88. {# Write a string to the assembler file }
  89. Procedure AsmWritePChar(p:pchar);
  90. {# Write a string to the assembler file followed by a new line }
  91. Procedure AsmWriteLn(const s:string);
  92. {# Write a new line to the assembler file }
  93. Procedure AsmLn;
  94. procedure AsmCreate(Aplace:tcutplace);
  95. procedure AsmClose;
  96. {# This routine should be overriden for each assembler, it is used
  97. to actually write the abstract assembler stream to file.}
  98. procedure WriteTree(p:TAAsmoutput);virtual;
  99. {# This routine should be overriden for each assembler, it is used
  100. to actually write all the different abstract assembler streams
  101. by calling for each stream type, the @var(WriteTree) method.}
  102. procedure WriteAsmList;virtual;
  103. public
  104. Constructor Create(smart:boolean);override;
  105. procedure MakeObject;override;
  106. end;
  107. TInternalAssembler=class(TAssembler)
  108. public
  109. constructor create(smart:boolean);override;
  110. destructor destroy;override;
  111. procedure MakeObject;override;
  112. protected
  113. objectdata : TAsmObjectData;
  114. objectoutput : tobjectoutput;
  115. private
  116. { the aasmoutput lists that need to be processed }
  117. lists : byte;
  118. list : array[1..maxoutputlists] of TAAsmoutput;
  119. { current processing }
  120. currlistidx : byte;
  121. currlist : TAAsmoutput;
  122. currpass : byte;
  123. procedure convertstab(p:pchar);
  124. function MaybeNextList(var hp:Tai):boolean;
  125. function TreePass0(hp:Tai):Tai;
  126. function TreePass1(hp:Tai):Tai;
  127. function TreePass2(hp:Tai):Tai;
  128. procedure writetree;
  129. procedure writetreesmart;
  130. end;
  131. TAssemblerClass = class of TAssembler;
  132. Procedure GenerateAsm(smart:boolean);
  133. Procedure OnlyAsm;
  134. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  135. procedure InitAssembler;
  136. procedure DoneAssembler;
  137. Implementation
  138. uses
  139. {$ifdef hasunix}
  140. {$ifdef havelinuxrtl10}
  141. linux,
  142. {$else}
  143. unix,
  144. {$endif}
  145. {$endif}
  146. cutils,script,fmodule,verbose,
  147. {$ifdef memdebug}
  148. cclasses,
  149. {$endif memdebug}
  150. {$ifdef m68k}
  151. cpuinfo,
  152. {$endif m68k}
  153. aasmcpu
  154. ;
  155. var
  156. CAssembler : array[tasm] of TAssemblerClass;
  157. {*****************************************************************************
  158. TAssembler
  159. *****************************************************************************}
  160. Constructor TAssembler.Create(smart:boolean);
  161. begin
  162. { load start values }
  163. asmfile:=current_module.get_asmfilename;
  164. objfile:=current_module.objfilename^;
  165. name:=Lower(current_module.modulename^);
  166. path:=current_module.outputpath^;
  167. asmprefix := current_module.asmprefix^;
  168. if not assigned(current_module.outputpath) then
  169. ppufilename := ''
  170. else
  171. ppufilename := current_module.ppufilename^;
  172. SmartAsm:=smart;
  173. SmartFilesCount:=0;
  174. SmartHeaderCount:=0;
  175. SmartLinkOFiles.Clear;
  176. end;
  177. Destructor TAssembler.Destroy;
  178. begin
  179. end;
  180. procedure TAssembler.NextSmartName(place:tcutplace);
  181. var
  182. s : string;
  183. begin
  184. inc(SmartFilesCount);
  185. if SmartFilesCount>999999 then
  186. Message(asmw_f_too_many_asm_files);
  187. case place of
  188. cut_begin :
  189. begin
  190. inc(SmartHeaderCount);
  191. s:=asmprefix+tostr(SmartHeaderCount)+'h';
  192. end;
  193. cut_normal :
  194. s:=asmprefix+tostr(SmartHeaderCount)+'s';
  195. cut_end :
  196. s:=asmprefix+tostr(SmartHeaderCount)+'t';
  197. end;
  198. AsmFile:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.asmext);
  199. ObjFile:=Path+FixFileName(s+tostr(SmartFilesCount)+target_info.objext);
  200. { insert in container so it can be cleared after the linking }
  201. SmartLinkOFiles.Insert(Objfile);
  202. end;
  203. {*****************************************************************************
  204. TExternalAssembler
  205. *****************************************************************************}
  206. Function DoPipe:boolean;
  207. begin
  208. DoPipe:=(cs_asm_pipe in aktglobalswitches) and
  209. not(cs_asm_leave in aktglobalswitches)
  210. and ((target_asm.id in [as_gas,as_darwin]));
  211. end;
  212. Constructor TExternalAssembler.Create(smart:boolean);
  213. begin
  214. inherited Create(smart);
  215. if SmartAsm then
  216. begin
  217. path:=FixPath(path+FixFileName(name)+target_info.smartext,false);
  218. CreateSmartLinkPath(path);
  219. end;
  220. Outcnt:=0;
  221. end;
  222. procedure TExternalAssembler.CreateSmartLinkPath(const s:string);
  223. var
  224. {$IFDEF USE_SYSUTILS}
  225. dir : TSearchRec;
  226. {$ELSE USE_SYSUTILS}
  227. dir : searchrec;
  228. {$ENDIF USE_SYSUTILS}
  229. hs : string;
  230. begin
  231. if PathExists(s) then
  232. begin
  233. { the path exists, now we clean only all the .o and .s files }
  234. { .o files }
  235. {$IFDEF USE_SYSUTILS}
  236. if findfirst(s+source_info.dirsep+'*'+target_info.objext,faAnyFile,dir) = 0
  237. then repeat
  238. RemoveFile(s+source_info.dirsep+dir.name);
  239. until findnext(dir) <> 0;
  240. {$ELSE USE_SYSUTILS}
  241. findfirst(s+source_info.dirsep+'*'+target_info.objext,anyfile,dir);
  242. while (doserror=0) do
  243. begin
  244. RemoveFile(s+source_info.dirsep+dir.name);
  245. findnext(dir);
  246. end;
  247. {$ENDIF USE_SYSUTILS}
  248. findclose(dir);
  249. { .s files }
  250. {$IFDEF USE_SYSUTILS}
  251. if findfirst(s+source_info.dirsep+'*'+target_info.asmext,faAnyFile,dir) = 0
  252. then repeat
  253. RemoveFile(s+source_info.dirsep+dir.name);
  254. until findnext(dir) <> 0;
  255. {$ELSE USE_SYSUTILS}
  256. findfirst(s+source_info.dirsep+'*'+target_info.asmext,anyfile,dir);
  257. while (doserror=0) do
  258. begin
  259. RemoveFile(s+source_info.dirsep+dir.name);
  260. findnext(dir);
  261. end;
  262. {$ENDIF USE_SYSUTILS}
  263. findclose(dir);
  264. end
  265. else
  266. begin
  267. hs:=s;
  268. if hs[length(hs)] in ['/','\'] then
  269. delete(hs,length(hs),1);
  270. {$I-}
  271. mkdir(hs);
  272. {$I+}
  273. if ioresult<>0 then;
  274. end;
  275. end;
  276. const
  277. lastas : byte=255;
  278. var
  279. LastASBin : pathstr;
  280. Function TExternalAssembler.FindAssembler:string;
  281. var
  282. asfound : boolean;
  283. UtilExe : string;
  284. begin
  285. asfound:=false;
  286. if cs_link_on_target in aktglobalswitches then
  287. begin
  288. { If linking on target, don't add any path PM }
  289. FindAssembler:=utilsprefix+AddExtension(target_asm.asmbin,target_info.exeext);
  290. exit;
  291. end
  292. else
  293. UtilExe:=utilsprefix+AddExtension(target_asm.asmbin,source_info.exeext);
  294. if lastas<>ord(target_asm.id) then
  295. begin
  296. lastas:=ord(target_asm.id);
  297. { is an assembler passed ? }
  298. if utilsdirectory<>'' then
  299. asfound:=FindFile(UtilExe,utilsdirectory,LastASBin);
  300. if not AsFound then
  301. asfound:=FindExe(UtilExe,LastASBin);
  302. if (not asfound) and not(cs_asm_extern in aktglobalswitches) then
  303. begin
  304. Message1(exec_e_assembler_not_found,LastASBin);
  305. aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
  306. end;
  307. if asfound then
  308. Message1(exec_t_using_assembler,LastASBin);
  309. end;
  310. FindAssembler:=LastASBin;
  311. end;
  312. Function TExternalAssembler.CallAssembler(const command:string; const para:TCmdStr):Boolean;
  313. {$IFDEF USE_SYSUTILS}
  314. var
  315. DosExitCode:Integer;
  316. {$ENDIF USE_SYSUTILS}
  317. begin
  318. callassembler:=true;
  319. if not(cs_asm_extern in aktglobalswitches) then
  320. {$IFDEF USE_SYSUTILS}
  321. try
  322. DosExitCode := ExecuteProcess(command,para);
  323. if DosExitCode <>0
  324. then begin
  325. Message1(exec_e_error_while_assembling,tostr(dosexitcode));
  326. callassembler:=false;
  327. end;
  328. except on E:EOSError do
  329. begin
  330. Message1(exec_e_cant_call_assembler,tostr(E.ErrorCode));
  331. aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
  332. callassembler:=false;
  333. end
  334. end
  335. {$ELSE USE_SYSUTILS}
  336. begin
  337. swapvectors;
  338. exec(maybequoted(command),para);
  339. swapvectors;
  340. if (doserror<>0) then
  341. begin
  342. Message1(exec_e_cant_call_assembler,tostr(doserror));
  343. aktglobalswitches:=aktglobalswitches+[cs_asm_extern];
  344. callassembler:=false;
  345. end
  346. else
  347. if (dosexitcode<>0) then
  348. begin
  349. Message1(exec_e_error_while_assembling,tostr(dosexitcode));
  350. callassembler:=false;
  351. end;
  352. end
  353. {$ENDIF USE_SYSUTILS}
  354. else
  355. AsmRes.AddAsmCommand(command,para,name);
  356. end;
  357. procedure TExternalAssembler.RemoveAsm;
  358. var
  359. g : file;
  360. begin
  361. if cs_asm_leave in aktglobalswitches then
  362. exit;
  363. if cs_asm_extern in aktglobalswitches then
  364. AsmRes.AddDeleteCommand(AsmFile)
  365. else
  366. begin
  367. assign(g,AsmFile);
  368. {$I-}
  369. erase(g);
  370. {$I+}
  371. if ioresult<>0 then;
  372. end;
  373. end;
  374. Function TExternalAssembler.DoAssemble:boolean;
  375. var
  376. s : TCmdStr;
  377. begin
  378. DoAssemble:=true;
  379. if DoPipe then
  380. exit;
  381. if not(cs_asm_extern in aktglobalswitches) then
  382. begin
  383. if SmartAsm then
  384. begin
  385. if (SmartFilesCount<=1) then
  386. Message1(exec_i_assembling_smart,name);
  387. end
  388. else
  389. Message1(exec_i_assembling,name);
  390. end;
  391. s:=target_asm.asmcmd;
  392. {$ifdef m68k}
  393. if aktoptprocessor = MC68020 then
  394. s:='-m68020 '+s
  395. else
  396. s:='-m68000 '+s;
  397. {$endif}
  398. if (cs_link_on_target in aktglobalswitches) then
  399. begin
  400. Replace(s,'$ASM',maybequoted(ScriptFixFileName(AsmFile)));
  401. Replace(s,'$OBJ',maybequoted(ScriptFixFileName(ObjFile)));
  402. end
  403. else
  404. begin
  405. Replace(s,'$ASM',maybequoted(AsmFile));
  406. Replace(s,'$OBJ',maybequoted(ObjFile));
  407. end;
  408. if CallAssembler(FindAssembler,s) then
  409. RemoveAsm
  410. else
  411. begin
  412. DoAssemble:=false;
  413. GenerateError;
  414. end;
  415. end;
  416. Procedure TExternalAssembler.AsmFlush;
  417. begin
  418. if outcnt>0 then
  419. begin
  420. { suppress i/o error }
  421. {$i-}
  422. BlockWrite(outfile,outbuf,outcnt);
  423. {$i+}
  424. ioerror:=ioerror or (ioresult<>0);
  425. outcnt:=0;
  426. end;
  427. end;
  428. Procedure TExternalAssembler.AsmClear;
  429. begin
  430. outcnt:=0;
  431. end;
  432. Procedure TExternalAssembler.AsmWrite(const s:string);
  433. begin
  434. if OutCnt+length(s)>=AsmOutSize then
  435. AsmFlush;
  436. Move(s[1],OutBuf[OutCnt],length(s));
  437. inc(OutCnt,length(s));
  438. inc(AsmSize,length(s));
  439. end;
  440. Procedure TExternalAssembler.AsmWriteLn(const s:string);
  441. begin
  442. AsmWrite(s);
  443. AsmLn;
  444. end;
  445. Procedure TExternalAssembler.AsmWritePChar(p:pchar);
  446. var
  447. i,j : longint;
  448. begin
  449. i:=StrLen(p);
  450. j:=i;
  451. while j>0 do
  452. begin
  453. i:=min(j,AsmOutSize);
  454. if OutCnt+i>=AsmOutSize then
  455. AsmFlush;
  456. Move(p[0],OutBuf[OutCnt],i);
  457. inc(OutCnt,i);
  458. inc(AsmSize,i);
  459. dec(j,i);
  460. p:=pchar(@p[i]);
  461. end;
  462. end;
  463. Procedure TExternalAssembler.AsmLn;
  464. begin
  465. if OutCnt>=AsmOutSize-2 then
  466. AsmFlush;
  467. if (cs_link_on_target in aktglobalswitches) then
  468. begin
  469. OutBuf[OutCnt]:=target_info.newline[1];
  470. inc(OutCnt);
  471. inc(AsmSize);
  472. if length(target_info.newline)>1 then
  473. begin
  474. OutBuf[OutCnt]:=target_info.newline[2];
  475. inc(OutCnt);
  476. inc(AsmSize);
  477. end;
  478. end
  479. else
  480. begin
  481. OutBuf[OutCnt]:=source_info.newline[1];
  482. inc(OutCnt);
  483. inc(AsmSize);
  484. if length(source_info.newline)>1 then
  485. begin
  486. OutBuf[OutCnt]:=source_info.newline[2];
  487. inc(OutCnt);
  488. inc(AsmSize);
  489. end;
  490. end;
  491. end;
  492. {$ifdef i386}
  493. const format_option='--32';
  494. {$else}{$ifdef x86_64}
  495. const format_option='--64';
  496. {$else}
  497. const format_option='';
  498. {$endif}{$endif}
  499. procedure TExternalAssembler.AsmCreate(Aplace:tcutplace);
  500. begin
  501. if SmartAsm then
  502. NextSmartName(Aplace);
  503. {$ifdef hasunix}
  504. if DoPipe then
  505. begin
  506. Message1(exec_i_assembling_pipe,asmfile);
  507. POpen(outfile,'as '+format_option+' -o '+objfile,'W');
  508. end
  509. else
  510. {$endif}
  511. begin
  512. Assign(outfile,asmfile);
  513. {$I-}
  514. Rewrite(outfile,1);
  515. {$I+}
  516. if ioresult<>0 then
  517. begin
  518. ioerror:=true;
  519. Message1(exec_d_cant_create_asmfile,asmfile);
  520. end;
  521. end;
  522. outcnt:=0;
  523. AsmSize:=0;
  524. AsmStartSize:=0;
  525. end;
  526. procedure TExternalAssembler.AsmClose;
  527. var
  528. f : file;
  529. FileAge : longint;
  530. begin
  531. AsmFlush;
  532. {$ifdef hasunix}
  533. if DoPipe then
  534. begin
  535. if PClose(outfile) <> 0 then
  536. GenerateError;
  537. end
  538. else
  539. {$endif}
  540. begin
  541. {Touch Assembler time to ppu time is there is a ppufilename}
  542. if ppufilename<>'' then
  543. begin
  544. Assign(f,ppufilename);
  545. {$I-}
  546. reset(f,1);
  547. {$I+}
  548. if ioresult=0 then
  549. begin
  550. {$IFDEF USE_SYSUTILS}
  551. FileAge := FileGetDate(GetFileHandle(f));
  552. {$ELSE USE_SYSUTILS}
  553. GetFTime(f, FileAge);
  554. {$ENDIF USE_SYSUTILS}
  555. close(f);
  556. reset(outfile,1);
  557. {$IFDEF USE_SYSUTILS}
  558. FileSetDate(GetFileHandle(outFile),FileAge);
  559. {$ELSE USE_SYSUTILS}
  560. SetFTime(f, FileAge);
  561. {$ENDIF USE_SYSUTILS}
  562. end;
  563. end;
  564. close(outfile);
  565. end;
  566. end;
  567. procedure TExternalAssembler.WriteTree(p:TAAsmoutput);
  568. begin
  569. end;
  570. procedure TExternalAssembler.WriteAsmList;
  571. begin
  572. end;
  573. procedure TExternalAssembler.MakeObject;
  574. begin
  575. AsmCreate(cut_normal);
  576. WriteAsmList;
  577. AsmClose;
  578. if not(ioerror) then
  579. DoAssemble;
  580. end;
  581. {*****************************************************************************
  582. TInternalAssembler
  583. *****************************************************************************}
  584. constructor TInternalAssembler.create(smart:boolean);
  585. begin
  586. inherited create(smart);
  587. objectoutput:=nil;
  588. objectdata:=nil;
  589. SmartAsm:=smart;
  590. currpass:=0;
  591. end;
  592. destructor TInternalAssembler.destroy;
  593. {$ifdef MEMDEBUG}
  594. var
  595. d : tmemdebug;
  596. {$endif}
  597. begin
  598. {$ifdef MEMDEBUG}
  599. d := tmemdebug.create(name+' - agbin');
  600. {$endif}
  601. objectdata.free;
  602. objectoutput.free;
  603. {$ifdef MEMDEBUG}
  604. d.free;
  605. {$endif}
  606. end;
  607. procedure TInternalAssembler.convertstab(p:pchar);
  608. function consumecomma(var p:pchar):boolean;
  609. begin
  610. while (p^=' ') do
  611. inc(p);
  612. result:=(p^=',');
  613. inc(p);
  614. end;
  615. function consumenumber(var p:pchar;out value:longint):boolean;
  616. var
  617. hs : string;
  618. len,
  619. code : integer;
  620. begin
  621. value:=0;
  622. while (p^=' ') do
  623. inc(p);
  624. len:=0;
  625. while (p^ in ['0'..'9']) do
  626. begin
  627. inc(len);
  628. hs[len]:=p^;
  629. inc(p);
  630. end;
  631. if len>0 then
  632. begin
  633. hs[0]:=chr(len);
  634. val(hs,value,code);
  635. end
  636. else
  637. code:=-1;
  638. result:=(code=0);
  639. end;
  640. function consumeoffset(var p:pchar;out relocsym:tasmsymbol;out value:longint):boolean;
  641. var
  642. hs : string;
  643. len,
  644. code : integer;
  645. pstart : pchar;
  646. sym : tasmsymbol;
  647. exprvalue : longint;
  648. gotmin,
  649. dosub : boolean;
  650. begin
  651. result:=false;
  652. value:=0;
  653. relocsym:=nil;
  654. gotmin:=false;
  655. repeat
  656. dosub:=false;
  657. exprvalue:=0;
  658. if gotmin then
  659. begin
  660. dosub:=true;
  661. gotmin:=false;
  662. end;
  663. while (p^=' ') do
  664. inc(p);
  665. case p^ of
  666. #0 :
  667. break;
  668. ' ' :
  669. inc(p);
  670. '0'..'9' :
  671. begin
  672. len:=0;
  673. while (p^ in ['0'..'9']) do
  674. begin
  675. inc(len);
  676. hs[len]:=p^;
  677. inc(p);
  678. end;
  679. hs[0]:=chr(len);
  680. val(hs,exprvalue,code);
  681. end;
  682. '.','_',
  683. 'A'..'Z',
  684. 'a'..'z' :
  685. begin
  686. pstart:=p;
  687. while not(p^ in [#0,' ','-','+']) do
  688. inc(p);
  689. len:=p-pstart;
  690. if len>255 then
  691. internalerror(200509187);
  692. move(pstart^,hs[1],len);
  693. hs[0]:=chr(len);
  694. sym:=objectlibrary.newasmsymbol(hs,AB_EXTERNAL,AT_NONE);
  695. if not assigned(sym) then
  696. internalerror(200509188);
  697. objectlibrary.UsedAsmSymbolListInsert(sym);
  698. { Second symbol? }
  699. if assigned(relocsym) then
  700. begin
  701. if (relocsym.section<>sym.section) then
  702. internalerror(2005091810);
  703. relocsym:=nil;
  704. end
  705. else
  706. begin
  707. relocsym:=sym;
  708. end;
  709. exprvalue:=sym.address;
  710. end;
  711. '+' :
  712. begin
  713. { nothing, by default addition is done }
  714. inc(p);
  715. end;
  716. '-' :
  717. begin
  718. gotmin:=true;
  719. inc(p);
  720. end;
  721. else
  722. internalerror(200509189);
  723. end;
  724. if dosub then
  725. dec(value,exprvalue)
  726. else
  727. inc(value,exprvalue);
  728. until false;
  729. result:=true;
  730. end;
  731. const
  732. N_Function = $24; { function or const }
  733. var
  734. ofs,
  735. nline,
  736. nidx,
  737. nother,
  738. i : longint;
  739. relocsym : tasmsymbol;
  740. pstr,
  741. pcurr,
  742. pendquote : pchar;
  743. begin
  744. pcurr:=nil;
  745. pstr:=nil;
  746. pendquote:=nil;
  747. { Parse string part }
  748. if p[0]='"' then
  749. begin
  750. pstr:=@p[1];
  751. { Ignore \" inside the string }
  752. i:=1;
  753. while not((p[i]='"') and (p[i-1]<>'\')) and
  754. (p[i]<>#0) do
  755. inc(i);
  756. pendquote:=@p[i];
  757. pendquote^:=#0;
  758. pcurr:=@p[i+1];
  759. if not consumecomma(pcurr) then
  760. internalerror(200509181);
  761. end
  762. else
  763. pcurr:=p;
  764. { When in pass 1 then only alloc and leave }
  765. if currpass=1 then
  766. objectdata.allocstab(pstr)
  767. else
  768. begin
  769. { Stabs format: nidx,nother,nline[,offset] }
  770. if not consumenumber(pcurr,nidx) then
  771. internalerror(200509182);
  772. if not consumecomma(pcurr) then
  773. internalerror(200509183);
  774. if not consumenumber(pcurr,nother) then
  775. internalerror(200509184);
  776. if not consumecomma(pcurr) then
  777. internalerror(200509185);
  778. if not consumenumber(pcurr,nline) then
  779. internalerror(200509186);
  780. if consumecomma(pcurr) then
  781. consumeoffset(pcurr,relocsym,ofs)
  782. else
  783. begin
  784. ofs:=0;
  785. relocsym:=nil;
  786. end;
  787. if (nidx=N_Function) and
  788. (tf_use_function_relative_addresses in target_info.flags) then
  789. ofs:=0;
  790. objectdata.writestab(ofs,relocsym,nidx,nother,nline,pstr);
  791. end;
  792. if assigned(pendquote) then
  793. pendquote^:='"';
  794. end;
  795. function TInternalAssembler.MaybeNextList(var hp:Tai):boolean;
  796. begin
  797. { maybe end of list }
  798. while not assigned(hp) do
  799. begin
  800. if currlistidx<lists then
  801. begin
  802. inc(currlistidx);
  803. currlist:=list[currlistidx];
  804. hp:=Tai(currList.first);
  805. end
  806. else
  807. begin
  808. MaybeNextList:=false;
  809. exit;
  810. end;
  811. end;
  812. MaybeNextList:=true;
  813. end;
  814. function TInternalAssembler.TreePass0(hp:Tai):Tai;
  815. var
  816. l : longint;
  817. begin
  818. while assigned(hp) do
  819. begin
  820. case hp.typ of
  821. ait_align :
  822. begin
  823. { always use the maximum fillsize in this pass to avoid possible
  824. short jumps to become out of range }
  825. Tai_align(hp).fillsize:=Tai_align(hp).aligntype;
  826. objectdata.alloc(Tai_align(hp).fillsize);
  827. end;
  828. ait_datablock :
  829. begin
  830. l:=used_align(size_2_align(Tai_datablock(hp).size),0,objectdata.currsec.addralign);
  831. if SmartAsm or (not Tai_datablock(hp).is_global) then
  832. begin
  833. objectdata.allocalign(l);
  834. objectdata.alloc(Tai_datablock(hp).size);
  835. end;
  836. end;
  837. ait_real_80bit :
  838. objectdata.alloc(10);
  839. ait_real_64bit :
  840. objectdata.alloc(8);
  841. ait_real_32bit :
  842. objectdata.alloc(4);
  843. ait_comp_64bit :
  844. objectdata.alloc(8);
  845. ait_const:
  846. objectdata.alloc(tai_const(hp).size);
  847. ait_section:
  848. begin
  849. objectdata.CreateSection(Tai_section(hp).sectype,Tai_section(hp).name^,Tai_section(hp).secalign,[]);
  850. Tai_section(hp).sec:=objectdata.CurrSec;
  851. end;
  852. ait_symbol :
  853. objectdata.allocsymbol(currpass,Tai_symbol(hp).sym,0);
  854. ait_label :
  855. objectdata.allocsymbol(currpass,Tai_label(hp).l,0);
  856. ait_string :
  857. objectdata.alloc(Tai_string(hp).len);
  858. ait_instruction :
  859. begin
  860. {$ifdef i386}
  861. {$ifndef NOAG386BIN}
  862. { reset instructions which could change in pass 2 }
  863. Taicpu(hp).resetpass2;
  864. objectdata.alloc(Taicpu(hp).Pass1(objectdata.currsec.datasize));
  865. {$endif NOAG386BIN}
  866. {$endif i386}
  867. {$ifdef arm}
  868. { reset instructions which could change in pass 2 }
  869. Taicpu(hp).resetpass2;
  870. objectdata.alloc(Taicpu(hp).Pass1(objectdata.currsec.datasize));
  871. {$endif arm}
  872. end;
  873. ait_cutobject :
  874. if SmartAsm then
  875. break;
  876. end;
  877. hp:=Tai(hp.next);
  878. end;
  879. TreePass0:=hp;
  880. end;
  881. function TInternalAssembler.TreePass1(hp:Tai):Tai;
  882. var
  883. InlineLevel,
  884. l,
  885. i : longint;
  886. begin
  887. inlinelevel:=0;
  888. while assigned(hp) do
  889. begin
  890. case hp.typ of
  891. ait_align :
  892. begin
  893. { here we must determine the fillsize which is used in pass2 }
  894. Tai_align(hp).fillsize:=align(objectdata.currsec.datasize,Tai_align(hp).aligntype)-
  895. objectdata.currsec.datasize;
  896. objectdata.alloc(Tai_align(hp).fillsize);
  897. end;
  898. ait_datablock :
  899. begin
  900. if not (objectdata.currsec.sectype in [sec_bss,sec_threadvar]) then
  901. Message(asmw_e_alloc_data_only_in_bss);
  902. l:=used_align(size_2_align(Tai_datablock(hp).size),0,objectdata.currsec.addralign);
  903. { if Tai_datablock(hp).is_global and
  904. not SmartAsm then
  905. begin}
  906. { objectdata.allocsymbol(currpass,Tai_datablock(hp).sym,Tai_datablock(hp).size);}
  907. { force to be common/external, must be after setaddress as that would
  908. set it to AB_GLOBAL }
  909. { Tai_datablock(hp).sym.currbind:=AB_COMMON;
  910. end
  911. else
  912. begin}
  913. objectdata.allocalign(l);
  914. objectdata.allocsymbol(currpass,Tai_datablock(hp).sym,Tai_datablock(hp).size);
  915. objectdata.alloc(Tai_datablock(hp).size);
  916. { end;}
  917. objectlibrary.UsedAsmSymbolListInsert(Tai_datablock(hp).sym);
  918. end;
  919. ait_real_80bit :
  920. objectdata.alloc(10);
  921. ait_real_64bit :
  922. objectdata.alloc(8);
  923. ait_real_32bit :
  924. objectdata.alloc(4);
  925. ait_comp_64bit :
  926. objectdata.alloc(8);
  927. ait_const:
  928. begin
  929. objectdata.alloc(tai_const(hp).size);
  930. if assigned(Tai_const(hp).sym) then
  931. objectlibrary.UsedAsmSymbolListInsert(Tai_const(hp).sym);
  932. if assigned(Tai_const(hp).endsym) then
  933. objectlibrary.UsedAsmSymbolListInsert(Tai_const(hp).endsym);
  934. end;
  935. ait_section:
  936. begin
  937. { use cached value }
  938. objectdata.setsection(Tai_section(hp).sec);
  939. end;
  940. ait_stab :
  941. begin
  942. if assigned(Tai_stab(hp).str) then
  943. convertstab(Tai_stab(hp).str);
  944. end;
  945. ait_function_name,
  946. ait_force_line : ;
  947. ait_symbol :
  948. begin
  949. objectdata.allocsymbol(currpass,Tai_symbol(hp).sym,0);
  950. objectlibrary.UsedAsmSymbolListInsert(Tai_symbol(hp).sym);
  951. end;
  952. ait_symbol_end :
  953. begin
  954. if target_info.system in [system_i386_linux,system_i386_beos] then
  955. begin
  956. Tai_symbol_end(hp).sym.size:=objectdata.currsec.datasize-Tai_symbol_end(hp).sym.address;
  957. objectlibrary.UsedAsmSymbolListInsert(Tai_symbol_end(hp).sym);
  958. end;
  959. end;
  960. ait_label :
  961. begin
  962. objectdata.allocsymbol(currpass,Tai_label(hp).l,0);
  963. objectlibrary.UsedAsmSymbolListInsert(Tai_label(hp).l);
  964. end;
  965. ait_string :
  966. objectdata.alloc(Tai_string(hp).len);
  967. ait_instruction :
  968. begin
  969. objectdata.alloc(Taicpu(hp).Pass1(objectdata.currsec.datasize));
  970. { fixup the references }
  971. for i:=1 to Taicpu(hp).ops do
  972. begin
  973. with Taicpu(hp).oper[i-1]^ do
  974. begin
  975. case typ of
  976. top_ref :
  977. begin
  978. if assigned(ref^.symbol) then
  979. objectlibrary.UsedAsmSymbolListInsert(ref^.symbol);
  980. if assigned(ref^.relsymbol) then
  981. objectlibrary.UsedAsmSymbolListInsert(ref^.symbol);
  982. end;
  983. end;
  984. end;
  985. end;
  986. end;
  987. ait_cutobject :
  988. if SmartAsm then
  989. break;
  990. ait_marker :
  991. if tai_marker(hp).kind=InlineStart then
  992. inc(InlineLevel)
  993. else if tai_marker(hp).kind=InlineEnd then
  994. dec(InlineLevel);
  995. end;
  996. hp:=Tai(hp.next);
  997. end;
  998. TreePass1:=hp;
  999. end;
  1000. function TInternalAssembler.TreePass2(hp:Tai):Tai;
  1001. var
  1002. fillbuffer : tfillbuffer;
  1003. InlineLevel,
  1004. l : longint;
  1005. v : int64;
  1006. {$ifdef x86}
  1007. co : comp;
  1008. {$endif x86}
  1009. begin
  1010. inlinelevel:=0;
  1011. { main loop }
  1012. while assigned(hp) do
  1013. begin
  1014. case hp.typ of
  1015. ait_align :
  1016. begin
  1017. if objectdata.currsec.sectype in [sec_bss,sec_threadvar] then
  1018. objectdata.alloc(Tai_align(hp).fillsize)
  1019. else
  1020. objectdata.writebytes(Tai_align(hp).calculatefillbuf(fillbuffer)^,Tai_align(hp).fillsize);
  1021. end;
  1022. ait_section :
  1023. begin
  1024. { use cached value }
  1025. objectdata.setsection(Tai_section(hp).sec);
  1026. end;
  1027. ait_symbol :
  1028. begin
  1029. objectdata.writesymbol(Tai_symbol(hp).sym);
  1030. objectoutput.exportsymbol(Tai_symbol(hp).sym);
  1031. end;
  1032. ait_datablock :
  1033. begin
  1034. l:=used_align(size_2_align(Tai_datablock(hp).size),0,objectdata.currsec.addralign);
  1035. objectdata.writesymbol(Tai_datablock(hp).sym);
  1036. objectoutput.exportsymbol(Tai_datablock(hp).sym);
  1037. { if SmartAsm or (not Tai_datablock(hp).is_global) then
  1038. begin}
  1039. objectdata.allocalign(l);
  1040. objectdata.alloc(Tai_datablock(hp).size);
  1041. { end;}
  1042. end;
  1043. ait_real_80bit :
  1044. objectdata.writebytes(Tai_real_80bit(hp).value,10);
  1045. ait_real_64bit :
  1046. objectdata.writebytes(Tai_real_64bit(hp).value,8);
  1047. ait_real_32bit :
  1048. objectdata.writebytes(Tai_real_32bit(hp).value,4);
  1049. ait_comp_64bit :
  1050. begin
  1051. {$ifdef x86}
  1052. co:=comp(Tai_comp_64bit(hp).value);
  1053. objectdata.writebytes(co,8);
  1054. {$endif x86}
  1055. end;
  1056. ait_string :
  1057. objectdata.writebytes(Tai_string(hp).str^,Tai_string(hp).len);
  1058. ait_const :
  1059. begin
  1060. case tai_const(hp).consttype of
  1061. aitconst_64bit,
  1062. aitconst_32bit,
  1063. aitconst_16bit,
  1064. aitconst_8bit :
  1065. if assigned(tai_const(hp).sym) then
  1066. begin
  1067. if assigned(tai_const(hp).endsym) then
  1068. begin
  1069. if tai_const(hp).endsym.section<>tai_const(hp).sym.section then
  1070. internalerror(200404124);
  1071. v:=tai_const(hp).endsym.address-tai_const(hp).sym.address+Tai_const(hp).value;
  1072. objectdata.writebytes(v,tai_const(hp).size);
  1073. end
  1074. else
  1075. objectdata.writereloc(Tai_const(hp).value,Tai_const(hp).size,
  1076. Tai_const(hp).sym,RELOC_ABSOLUTE);
  1077. end
  1078. else
  1079. objectdata.writebytes(Tai_const(hp).value,tai_const(hp).size);
  1080. aitconst_rva_symbol :
  1081. objectdata.writereloc(Tai_const(hp).value,sizeof(aint),Tai_const(hp).sym,RELOC_RVA);
  1082. end;
  1083. end;
  1084. ait_label :
  1085. begin
  1086. objectdata.writesymbol(Tai_label(hp).l);
  1087. { exporting shouldn't be necessary as labels are local,
  1088. but it's better to be on the safe side (PFV) }
  1089. objectoutput.exportsymbol(Tai_label(hp).l);
  1090. end;
  1091. ait_instruction :
  1092. Taicpu(hp).Pass2(objectdata);
  1093. ait_stab :
  1094. convertstab(Tai_stab(hp).str);
  1095. ait_function_name,
  1096. ait_force_line : ;
  1097. ait_cutobject :
  1098. if SmartAsm then
  1099. break;
  1100. ait_marker :
  1101. if tai_marker(hp).kind=InlineStart then
  1102. inc(InlineLevel)
  1103. else if tai_marker(hp).kind=InlineEnd then
  1104. dec(InlineLevel);
  1105. end;
  1106. hp:=Tai(hp.next);
  1107. end;
  1108. TreePass2:=hp;
  1109. end;
  1110. procedure TInternalAssembler.writetree;
  1111. var
  1112. hp : Tai;
  1113. label
  1114. doexit;
  1115. begin
  1116. objectdata:=objectoutput.newobjectdata(Objfile);
  1117. { reset the asmsymbol list }
  1118. objectlibrary.CreateUsedAsmsymbolList;
  1119. { Pass 0 }
  1120. currpass:=0;
  1121. objectdata.createsection(sec_code,'',0,[]);
  1122. objectdata.beforealloc;
  1123. { start with list 1 }
  1124. currlistidx:=1;
  1125. currlist:=list[currlistidx];
  1126. hp:=Tai(currList.first);
  1127. while assigned(hp) do
  1128. begin
  1129. hp:=TreePass0(hp);
  1130. MaybeNextList(hp);
  1131. end;
  1132. objectdata.afteralloc;
  1133. { leave if errors have occured }
  1134. if errorcount>0 then
  1135. goto doexit;
  1136. { Pass 1 }
  1137. currpass:=1;
  1138. objectdata.resetsections;
  1139. objectdata.beforealloc;
  1140. objectdata.createsection(sec_code,'',0,[]);
  1141. { start with list 1 }
  1142. currlistidx:=1;
  1143. currlist:=list[currlistidx];
  1144. hp:=Tai(currList.first);
  1145. while assigned(hp) do
  1146. begin
  1147. hp:=TreePass1(hp);
  1148. MaybeNextList(hp);
  1149. end;
  1150. objectdata.createsection(sec_code,'',0,[]);
  1151. objectdata.afteralloc;
  1152. { check for undefined labels and reset }
  1153. objectlibrary.UsedAsmSymbolListCheckUndefined;
  1154. { leave if errors have occured }
  1155. if errorcount>0 then
  1156. goto doexit;
  1157. { Pass 2 }
  1158. currpass:=2;
  1159. objectdata.resetsections;
  1160. objectdata.beforewrite;
  1161. objectdata.createsection(sec_code,'',0,[]);
  1162. { start with list 1 }
  1163. currlistidx:=1;
  1164. currlist:=list[currlistidx];
  1165. hp:=Tai(currList.first);
  1166. while assigned(hp) do
  1167. begin
  1168. hp:=TreePass2(hp);
  1169. MaybeNextList(hp);
  1170. end;
  1171. objectdata.createsection(sec_code,'',0,[]);
  1172. objectdata.afterwrite;
  1173. { don't write the .o file if errors have occured }
  1174. if errorcount=0 then
  1175. begin
  1176. { write objectfile }
  1177. objectoutput.startobjectfile(ObjFile);
  1178. objectoutput.writeobjectfile(objectdata);
  1179. objectdata.free;
  1180. objectdata:=nil;
  1181. end;
  1182. doexit:
  1183. { reset the used symbols back, must be after the .o has been
  1184. written }
  1185. objectlibrary.UsedAsmsymbolListReset;
  1186. objectlibrary.DestroyUsedAsmsymbolList;
  1187. end;
  1188. procedure TInternalAssembler.writetreesmart;
  1189. var
  1190. hp : Tai;
  1191. startsectype : TAsmSectionType;
  1192. place: tcutplace;
  1193. begin
  1194. NextSmartName(cut_normal);
  1195. objectdata:=objectoutput.newobjectdata(Objfile);
  1196. startsectype:=sec_code;
  1197. { start with list 1 }
  1198. currlistidx:=1;
  1199. currlist:=list[currlistidx];
  1200. hp:=Tai(currList.first);
  1201. while assigned(hp) do
  1202. begin
  1203. { reset the asmsymbol list }
  1204. objectlibrary.CreateUsedAsmSymbolList;
  1205. { Pass 0 }
  1206. currpass:=0;
  1207. objectdata.resetsections;
  1208. objectdata.beforealloc;
  1209. objectdata.createsection(startsectype,'',0,[]);
  1210. TreePass0(hp);
  1211. objectdata.afteralloc;
  1212. { leave if errors have occured }
  1213. if errorcount>0 then
  1214. exit;
  1215. { Pass 1 }
  1216. currpass:=1;
  1217. objectdata.resetsections;
  1218. objectdata.beforealloc;
  1219. objectdata.createsection(startsectype,'',0,[]);
  1220. TreePass1(hp);
  1221. objectdata.afteralloc;
  1222. { check for undefined labels }
  1223. objectlibrary.UsedAsmSymbolListCheckUndefined;
  1224. { leave if errors have occured }
  1225. if errorcount>0 then
  1226. exit;
  1227. { Pass 2 }
  1228. currpass:=2;
  1229. objectoutput.startobjectfile(Objfile);
  1230. objectdata.resetsections;
  1231. objectdata.beforewrite;
  1232. objectdata.createsection(startsectype,'',0,[]);
  1233. hp:=TreePass2(hp);
  1234. { save section type for next loop, must be done before EndFileLineInfo
  1235. because that changes the section to sec_code }
  1236. startsectype:=objectdata.currsec.sectype;
  1237. objectdata.afterwrite;
  1238. { leave if errors have occured }
  1239. if errorcount>0 then
  1240. exit;
  1241. { write the current objectfile }
  1242. objectoutput.writeobjectfile(objectdata);
  1243. objectdata.free;
  1244. objectdata:=nil;
  1245. { reset the used symbols back, must be after the .o has been
  1246. written }
  1247. objectlibrary.UsedAsmsymbolListReset;
  1248. objectlibrary.DestroyUsedAsmsymbolList;
  1249. { end of lists? }
  1250. if not MaybeNextList(hp) then
  1251. break;
  1252. { we will start a new objectfile so reset everything }
  1253. { The place can still change in the next while loop, so don't init }
  1254. { the writer yet (JM) }
  1255. if (hp.typ=ait_cutobject) then
  1256. place := Tai_cutobject(hp).place
  1257. else
  1258. place := cut_normal;
  1259. { avoid empty files }
  1260. while assigned(hp) and
  1261. (Tai(hp).typ in [ait_marker,ait_comment,ait_section,ait_cutobject]) do
  1262. begin
  1263. if Tai(hp).typ=ait_section then
  1264. startsectype:=Tai_section(hp).sectype
  1265. else if (Tai(hp).typ=ait_cutobject) then
  1266. place:=Tai_cutobject(hp).place;
  1267. hp:=Tai(hp.next);
  1268. end;
  1269. { there is a problem if startsectype is sec_none !! PM }
  1270. if startsectype=sec_none then
  1271. startsectype:=sec_code;
  1272. if not MaybeNextList(hp) then
  1273. break;
  1274. { start next objectfile }
  1275. NextSmartName(place);
  1276. objectdata:=objectoutput.newobjectdata(Objfile);
  1277. end;
  1278. end;
  1279. procedure TInternalAssembler.MakeObject;
  1280. var to_do:set of Tasmlist;
  1281. i:Tasmlist;
  1282. procedure addlist(p:TAAsmoutput);
  1283. begin
  1284. inc(lists);
  1285. list[lists]:=p;
  1286. end;
  1287. begin
  1288. to_do:=[low(Tasmlist)..high(Tasmlist)];
  1289. if usedeffileforexports then
  1290. exclude(to_do,al_exports);
  1291. {$warning TODO internal writer support for dwarf}
  1292. exclude(to_do,al_dwarf);
  1293. if not(tf_section_threadvars in target_info.flags) then
  1294. exclude(to_do,al_threadvars);
  1295. for i:=low(Tasmlist) to high(Tasmlist) do
  1296. if (i in to_do) and (asmlist[i]<>nil) then
  1297. addlist(asmlist[i]);
  1298. if SmartAsm then
  1299. writetreesmart
  1300. else
  1301. writetree;
  1302. end;
  1303. {*****************************************************************************
  1304. Generate Assembler Files Main Procedure
  1305. *****************************************************************************}
  1306. Procedure GenerateAsm(smart:boolean);
  1307. var
  1308. a : TAssembler;
  1309. begin
  1310. if not assigned(CAssembler[target_asm.id]) then
  1311. Message(asmw_f_assembler_output_not_supported);
  1312. a:=CAssembler[target_asm.id].Create(smart);
  1313. a.MakeObject;
  1314. a.Free;
  1315. end;
  1316. Procedure OnlyAsm;
  1317. var
  1318. a : TExternalAssembler;
  1319. begin
  1320. a:=TExternalAssembler.Create(false);
  1321. a.DoAssemble;
  1322. a.Free;
  1323. end;
  1324. {*****************************************************************************
  1325. Init/Done
  1326. *****************************************************************************}
  1327. procedure RegisterAssembler(const r:tasminfo;c:TAssemblerClass);
  1328. var
  1329. t : tasm;
  1330. begin
  1331. t:=r.id;
  1332. if assigned(asminfos[t]) then
  1333. writeln('Warning: Assembler is already registered!')
  1334. else
  1335. Getmem(asminfos[t],sizeof(tasminfo));
  1336. asminfos[t]^:=r;
  1337. CAssembler[t]:=c;
  1338. end;
  1339. procedure InitAssembler;
  1340. begin
  1341. end;
  1342. procedure DoneAssembler;
  1343. begin
  1344. end;
  1345. end.