rautils.pas 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Carl Eric Codere and Peter Vreman
  4. This unit implements some support routines for assembler parsing
  5. independent of the processor
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. **********************************************************************}
  18. Unit RAUtils;
  19. Interface
  20. Uses
  21. strings,
  22. cobjects,
  23. globtype,types,systems,verbose,globals,files,
  24. symconst,symtable,aasm,cpubase,cpuasm
  25. {$ifdef NEWCG}
  26. ,cgbase
  27. {$else}
  28. ,hcodegen
  29. {$endif}
  30. ;
  31. Const
  32. RPNMax = 10; { I think you only need 4, but just to be safe }
  33. OpMax = 25;
  34. maxoperands = 3; { Maximum operands for assembler instructions }
  35. {---------------------------------------------------------------------
  36. Local Label Management
  37. ---------------------------------------------------------------------}
  38. Type
  39. { Each local label has this structure associated with it }
  40. PLocalLabel = ^TLocalLabel;
  41. TLocalLabel = object(TNamedIndexObject)
  42. Emitted : boolean;
  43. constructor Init(const n:string);
  44. function Getpasmlabel:pasmlabel;
  45. private
  46. lab : pasmlabel;
  47. end;
  48. PLocalLabelList = ^TLocalLabelList;
  49. TLocalLabelList = Object(TDictionary)
  50. procedure CheckEmitted;
  51. end;
  52. var
  53. LocalLabelList : PLocalLabelList;
  54. function CreateLocalLabel(const s: string; var hl: pasmlabel; emit:boolean):boolean;
  55. Function SearchLabel(const s: string; var hl: pasmlabel;emit:boolean): boolean;
  56. {---------------------------------------------------------------------
  57. Instruction management
  58. ---------------------------------------------------------------------}
  59. type
  60. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_REFERENCE,OPR_REGISTER);
  61. TOprRec = record
  62. case typ:TOprType of
  63. OPR_NONE : ();
  64. OPR_CONSTANT : (val:longint);
  65. OPR_SYMBOL : (symbol:PAsmSymbol;symofs:longint);
  66. OPR_REFERENCE : (ref:treference);
  67. OPR_REGISTER : (reg:tregister);
  68. end;
  69. POperand = ^TOperand;
  70. TOperand = object
  71. size : topsize;
  72. hastype, { if the operand has typecasted variable }
  73. hasvar : boolean; { if the operand is loaded with a variable }
  74. opr : TOprRec;
  75. constructor init;
  76. destructor done;virtual;
  77. Procedure BuildOperand;virtual;
  78. Procedure SetSize(_size:longint;force:boolean);
  79. Procedure SetCorrectSize(opcode:tasmop);virtual;
  80. Function SetupResult:boolean;
  81. Function SetupSelf:boolean;
  82. Function SetupOldEBP:boolean;
  83. Function SetupVar(const hs:string;GetOffset : boolean): Boolean;
  84. Function SetupDirectVar(const hs:string): Boolean;
  85. Procedure InitRef;
  86. end;
  87. PInstruction = ^TInstruction;
  88. TInstruction = object
  89. opcode : tasmop;
  90. opsize : topsize;
  91. condition : tasmcond;
  92. ops : byte;
  93. operands : array[1..maxoperands] of POperand;
  94. constructor init;
  95. destructor done;virtual;
  96. Procedure InitOperands;virtual;
  97. Procedure BuildOpcode;virtual;
  98. procedure ConcatInstruction(p:PAasmoutput);virtual;
  99. Procedure SwapOperands;
  100. end;
  101. {---------------------------------------------------------------------}
  102. { Expression parser types }
  103. {---------------------------------------------------------------------}
  104. TExprOperator = record
  105. ch: char; { operator }
  106. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  107. end;
  108. String15 = String[15];
  109. {**********************************************************************}
  110. { The following operators are supported: }
  111. { '+' : addition }
  112. { '-' : subtraction }
  113. { '*' : multiplication }
  114. { '/' : modulo division }
  115. { '^' : exclusive or }
  116. { '<' : shift left }
  117. { '>' : shift right }
  118. { '&' : bitwise and }
  119. { '|' : bitwise or }
  120. { '~' : bitwise complement }
  121. { '%' : modulo division }
  122. { nnn: longint numbers }
  123. { ( and ) parenthesis }
  124. {**********************************************************************}
  125. TExprParse = Object
  126. public
  127. Constructor Init;
  128. Destructor Done;
  129. Function Evaluate(Expr: String): longint;
  130. Function Priority(_Operator: Char): Integer; virtual;
  131. private
  132. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  133. RPNTop : Integer;
  134. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  135. OpTop : Integer;
  136. Procedure RPNPush(Num: Longint);
  137. Function RPNPop: Longint;
  138. Procedure RPNCalc(token: String15; prefix: boolean);
  139. Procedure OpPush(_Operator: char; prefix: boolean);
  140. { In reality returns TExprOperaotr }
  141. Procedure OpPop(var _Operator:TExprOperator);
  142. end;
  143. { Evaluate an expression string to a longint }
  144. Function CalculateExpression(const expression: string): longint;
  145. {---------------------------------------------------------------------}
  146. { String routines }
  147. {---------------------------------------------------------------------}
  148. Function ValDecimal(const S:String):longint;
  149. Function ValOctal(const S:String):longint;
  150. Function ValBinary(const S:String):longint;
  151. Function ValHexaDecimal(const S:String):longint;
  152. Function PadZero(Var s: String; n: byte): Boolean;
  153. Function EscapeToPascal(const s:string): string;
  154. {---------------------------------------------------------------------
  155. Symbol helper routines
  156. ---------------------------------------------------------------------}
  157. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  158. Function SearchType(const hs:string): Boolean;
  159. Function SearchRecordType(const s:string): boolean;
  160. Function SearchIConstant(const s:string; var l:longint): boolean;
  161. {---------------------------------------------------------------------
  162. Instruction generation routines
  163. ---------------------------------------------------------------------}
  164. Procedure ConcatPasString(p : paasmoutput;s:string);
  165. Procedure ConcatDirect(p : paasmoutput;s:string);
  166. Procedure ConcatLabel(p: paasmoutput;var l : pasmlabel);
  167. Procedure ConcatConstant(p : paasmoutput;value: longint; maxvalue: longint);
  168. Procedure ConcatConstSymbol(p : paasmoutput;const sym:string;l:longint);
  169. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  170. Procedure ConcatString(p : paasmoutput;s:string);
  171. procedure ConcatAlign(p:paasmoutput;l:longint);
  172. Procedure ConcatPublic(p:paasmoutput;const s : string);
  173. Procedure ConcatLocal(p:paasmoutput;const s : string);
  174. Procedure ConcatGlobalBss(const s : string;size : longint);
  175. Procedure ConcatLocalBss(const s : string;size : longint);
  176. Implementation
  177. {*************************************************************************
  178. TExprParse
  179. *************************************************************************}
  180. Constructor TExprParse.Init;
  181. Begin
  182. end;
  183. Procedure TExprParse.RPNPush(Num : longint);
  184. { Add an operand to the top of the RPN stack }
  185. begin
  186. if RPNTop < RPNMax then
  187. begin
  188. Inc(RPNTop);
  189. RPNStack[RPNTop]:=Num;
  190. end
  191. else
  192. Message(asmr_e_expr_illegal);
  193. end;
  194. Function TExprParse.RPNPop : longint;
  195. { Get the operand at the top of the RPN stack }
  196. begin
  197. if RPNTop > 0 then
  198. begin
  199. RPNPop:=RPNStack[RPNTop];
  200. Dec(RPNTop);
  201. end
  202. else
  203. Message(asmr_e_expr_illegal);
  204. end;
  205. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  206. Var
  207. Temp : longint;
  208. n1,n2 : longint;
  209. LocalError : Integer;
  210. begin
  211. { Handle operators }
  212. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  213. Case Token[1] of
  214. '+' :
  215. Begin
  216. if not prefix then
  217. RPNPush(RPNPop + RPNPop);
  218. end;
  219. '-' :
  220. Begin
  221. if prefix then
  222. RPNPush(-(RPNPop))
  223. else
  224. begin
  225. n1:=RPNPop;
  226. n2:=RPNPop;
  227. RPNPush(n2 - n1);
  228. end;
  229. end;
  230. '*' : RPNPush(RPNPop * RPNPop);
  231. '&' :
  232. begin
  233. n1:=RPNPop;
  234. n2:=RPNPop;
  235. RPNPush(n2 and n1);
  236. end;
  237. '|' :
  238. begin
  239. n1:=RPNPop;
  240. n2:=RPNPop;
  241. RPNPush(n2 or n1);
  242. end;
  243. '~' : RPNPush(NOT RPNPop);
  244. '<' :
  245. begin
  246. n1:=RPNPop;
  247. n2:=RPNPop;
  248. RPNPush(n2 SHL n1);
  249. end;
  250. '>' :
  251. begin
  252. n1:=RPNPop;
  253. n2:=RPNPop;
  254. RPNPush(n2 SHR n1);
  255. end;
  256. '%' :
  257. begin
  258. Temp:=RPNPop;
  259. if Temp <> 0 then
  260. RPNPush(RPNPop mod Temp)
  261. else
  262. begin
  263. Message(asmr_e_expr_zero_divide);
  264. { push 1 for error recovery }
  265. RPNPush(1);
  266. end;
  267. end;
  268. '^' : RPNPush(RPNPop XOR RPNPop);
  269. '/' :
  270. begin
  271. Temp:=RPNPop;
  272. if Temp <> 0 then
  273. RPNPush(RPNPop div Temp)
  274. else
  275. begin
  276. Message(asmr_e_expr_zero_divide);
  277. { push 1 for error recovery }
  278. RPNPush(1);
  279. end;
  280. end;
  281. end
  282. else
  283. begin
  284. { Convert String to number and add to stack }
  285. if token='-2147483648' then
  286. begin
  287. temp:=$80000000;
  288. localerror:=0;
  289. end
  290. else
  291. Val(Token, Temp, LocalError);
  292. if LocalError = 0 then
  293. RPNPush(Temp)
  294. else
  295. begin
  296. Message(asmr_e_expr_illegal);
  297. { push 1 for error recovery }
  298. RPNPush(1);
  299. end;
  300. end;
  301. end;
  302. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  303. { Add an operator onto top of the stack }
  304. begin
  305. if OpTop < OpMax then
  306. begin
  307. Inc(OpTop);
  308. OpStack[OpTop].ch:=_Operator;
  309. OpStack[OpTop].is_prefix:=prefix;
  310. end
  311. else
  312. Message(asmr_e_expr_illegal);
  313. end;
  314. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  315. { Get operator at the top of the stack }
  316. begin
  317. if OpTop > 0 then
  318. begin
  319. _Operator:=OpStack[OpTop];
  320. Dec(OpTop);
  321. end
  322. else
  323. Message(asmr_e_expr_illegal);
  324. end;
  325. Function TExprParse.Priority(_Operator : Char) : Integer;
  326. { Return priority of operator }
  327. { The greater the priority, the higher the precedence }
  328. begin
  329. Case _Operator OF
  330. '(' :
  331. Priority:=0;
  332. '+', '-' :
  333. Priority:=1;
  334. '*', '/','%','<','>' :
  335. Priority:=2;
  336. '|','&','^','~' :
  337. Priority:=0;
  338. else
  339. Message(asmr_e_expr_illegal);
  340. end;
  341. end;
  342. Function TExprParse.Evaluate(Expr : String):longint;
  343. Var
  344. I : Integer;
  345. Token : String15;
  346. opr : TExprOperator;
  347. begin
  348. Evaluate:=0;
  349. { Reset stacks }
  350. OpTop :=0;
  351. RPNTop:=0;
  352. Token :='';
  353. { nothing to do ? }
  354. if Expr='' then
  355. exit;
  356. For I:=1 to Length(Expr) DO
  357. begin
  358. if Expr[I] in ['0'..'9'] then
  359. begin { Build multi-digit numbers }
  360. Token:=Token + Expr[I];
  361. if I = Length(Expr) then { Send last one to calculator }
  362. RPNCalc(Token,false);
  363. end
  364. else
  365. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  366. begin
  367. if Token <> '' then
  368. begin { Send last built number to calc. }
  369. RPNCalc(Token,false);
  370. Token:='';
  371. end;
  372. Case Expr[I] OF
  373. '(' : OpPush('(',false);
  374. ')' : begin
  375. While OpStack[OpTop].ch <> '(' DO
  376. Begin
  377. OpPop(opr);
  378. RPNCalc(opr.ch,opr.is_prefix);
  379. end;
  380. OpPop(opr); { Pop off and ignore the '(' }
  381. end;
  382. '+','-','~' : Begin
  383. { workaround for -2147483648 }
  384. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  385. begin
  386. token:='-';
  387. expr[i]:='+';
  388. end;
  389. { if start of expression then surely a prefix }
  390. { or if previous char was also an operator }
  391. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  392. OpPush(Expr[I],true)
  393. else
  394. Begin
  395. { Evaluate all higher priority operators }
  396. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  397. Begin
  398. OpPop(opr);
  399. RPNCalc(opr.ch,opr.is_prefix);
  400. end;
  401. OpPush(Expr[I],false);
  402. End;
  403. end;
  404. '*', '/',
  405. '^','|','&',
  406. '%','<','>' : begin
  407. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  408. Begin
  409. OpPop(opr);
  410. RPNCalc(opr.ch,opr.is_prefix);
  411. end;
  412. OpPush(Expr[I],false);
  413. end;
  414. end; { Case }
  415. end
  416. else
  417. Message(asmr_e_expr_illegal); { Handle bad input error }
  418. end;
  419. { Pop off the remaining operators }
  420. While OpTop > 0 do
  421. Begin
  422. OpPop(opr);
  423. RPNCalc(opr.ch,opr.is_prefix);
  424. end;
  425. { The result is stored on the top of the stack }
  426. Evaluate:=RPNPop;
  427. end;
  428. Destructor TExprParse.Done;
  429. Begin
  430. end;
  431. Function CalculateExpression(const expression: string): longint;
  432. var
  433. expr: TExprParse;
  434. Begin
  435. expr.Init;
  436. CalculateExpression:=expr.Evaluate(expression);
  437. expr.Done;
  438. end;
  439. {*************************************************************************}
  440. { String conversions/utils }
  441. {*************************************************************************}
  442. Function EscapeToPascal(const s:string): string;
  443. { converts a C styled string - which contains escape }
  444. { characters to a pascal style string. }
  445. var
  446. i,len : longint;
  447. hs : string;
  448. temp : string;
  449. c : char;
  450. Begin
  451. hs:='';
  452. len:=0;
  453. i:=0;
  454. while (i<length(s)) and (len<255) do
  455. begin
  456. Inc(i);
  457. if (s[i]='\') and (i<length(s)) then
  458. Begin
  459. inc(i);
  460. case s[i] of
  461. '\' :
  462. c:='\';
  463. 'b':
  464. c:=#8;
  465. 'f':
  466. c:=#12;
  467. 'n':
  468. c:=#10;
  469. 'r':
  470. c:=#13;
  471. 't':
  472. c:=#9;
  473. '"':
  474. c:='"';
  475. '0'..'7':
  476. Begin
  477. temp:=s[i];
  478. temp:=temp+s[i+1];
  479. temp:=temp+s[i+2];
  480. inc(i,2);
  481. c:=chr(ValOctal(temp));
  482. end;
  483. 'x':
  484. Begin
  485. temp:=s[i+1];
  486. temp:=temp+s[i+2];
  487. inc(i,2);
  488. c:=chr(ValHexaDecimal(temp));
  489. end;
  490. else
  491. Begin
  492. Message1(asmr_e_escape_seq_ignored,s[i]);
  493. c:=s[i];
  494. end;
  495. end;
  496. end
  497. else
  498. c:=s[i];
  499. inc(len);
  500. hs[len]:=c;
  501. end;
  502. hs[0]:=chr(len);
  503. EscapeToPascal:=hs;
  504. end;
  505. Function ValDecimal(const S:String):longint;
  506. { Converts a decimal string to longint }
  507. var
  508. vs,c : longint;
  509. Begin
  510. vs:=0;
  511. for c:=1 to length(s) do
  512. begin
  513. vs:=vs*10;
  514. if s[c] in ['0'..'9'] then
  515. inc(vs,ord(s[c])-ord('0'))
  516. else
  517. begin
  518. Message1(asmr_e_error_converting_decimal,s);
  519. ValDecimal:=0;
  520. exit;
  521. end;
  522. end;
  523. ValDecimal:=vs;
  524. end;
  525. Function ValOctal(const S:String):longint;
  526. { Converts an octal string to longint }
  527. var
  528. vs,c : longint;
  529. Begin
  530. vs:=0;
  531. for c:=1 to length(s) do
  532. begin
  533. vs:=vs shl 3;
  534. if s[c] in ['0'..'7'] then
  535. inc(vs,ord(s[c])-ord('0'))
  536. else
  537. begin
  538. Message1(asmr_e_error_converting_octal,s);
  539. ValOctal:=0;
  540. exit;
  541. end;
  542. end;
  543. ValOctal:=vs;
  544. end;
  545. Function ValBinary(const S:String):longint;
  546. { Converts a binary string to longint }
  547. var
  548. vs,c : longint;
  549. Begin
  550. vs:=0;
  551. for c:=1 to length(s) do
  552. begin
  553. vs:=vs shl 1;
  554. if s[c] in ['0'..'1'] then
  555. inc(vs,ord(s[c])-ord('0'))
  556. else
  557. begin
  558. Message1(asmr_e_error_converting_binary,s);
  559. ValBinary:=0;
  560. exit;
  561. end;
  562. end;
  563. ValBinary:=vs;
  564. end;
  565. Function ValHexadecimal(const S:String):longint;
  566. { Converts a binary string to longint }
  567. var
  568. vs,c : longint;
  569. Begin
  570. vs:=0;
  571. for c:=1 to length(s) do
  572. begin
  573. vs:=vs shl 4;
  574. case s[c] of
  575. '0'..'9' :
  576. inc(vs,ord(s[c])-ord('0'));
  577. 'A'..'F' :
  578. inc(vs,ord(s[c])-ord('A')+10);
  579. 'a'..'f' :
  580. inc(vs,ord(s[c])-ord('a')+10);
  581. else
  582. begin
  583. Message1(asmr_e_error_converting_hexadecimal,s);
  584. ValHexadecimal:=0;
  585. exit;
  586. end;
  587. end;
  588. end;
  589. ValHexadecimal:=vs;
  590. end;
  591. Function PadZero(Var s: String; n: byte): Boolean;
  592. Begin
  593. PadZero:=TRUE;
  594. { Do some error checking first }
  595. if Length(s) = n then
  596. exit
  597. else
  598. if Length(s) > n then
  599. Begin
  600. PadZero:=FALSE;
  601. delete(s,n+1,length(s));
  602. exit;
  603. end
  604. else
  605. PadZero:=TRUE;
  606. { Fill it up with the specified character }
  607. fillchar(s[length(s)+1],n-1,#0);
  608. s[0]:=chr(n);
  609. end;
  610. {****************************************************************************
  611. TOperand
  612. ****************************************************************************}
  613. constructor TOperand.init;
  614. begin
  615. size:=S_NO;
  616. hastype:=false;
  617. hasvar:=false;
  618. FillChar(Opr,sizeof(Opr),0);
  619. end;
  620. destructor TOperand.done;
  621. begin
  622. end;
  623. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  624. begin
  625. end;
  626. Procedure TOperand.SetSize(_size:longint;force:boolean);
  627. begin
  628. if force or
  629. ((size = S_NO) and (_size<=extended_size)) then
  630. Begin
  631. case _size of
  632. 1 : size:=S_B;
  633. 2 : size:=S_W{ could be S_IS};
  634. 4 : size:=S_L{ could be S_IL or S_FS};
  635. 8 : size:=S_IQ{ could be S_D or S_FL};
  636. extended_size : size:=S_FX;
  637. end;
  638. end;
  639. end;
  640. Function TOperand.SetupResult:boolean;
  641. Begin
  642. SetupResult:=false;
  643. { replace by correct offset. }
  644. if assigned(procinfo^.returntype.def) and
  645. (procinfo^.returntype.def<>pdef(voiddef)) then
  646. begin
  647. opr.ref.offset:=procinfo^.return_offset;
  648. opr.ref.base:= procinfo^.framepointer;
  649. { always assume that the result is valid. }
  650. procinfo^.funcret_state:=vs_assigned;
  651. SetupResult:=true;
  652. end
  653. else
  654. Message(asmr_e_void_function);
  655. end;
  656. Function TOperand.SetupSelf:boolean;
  657. Begin
  658. SetupSelf:=false;
  659. if assigned(procinfo^._class) then
  660. Begin
  661. opr.typ:=OPR_REFERENCE;
  662. opr.ref.offset:=procinfo^.selfpointer_offset;
  663. opr.ref.base:=procinfo^.framepointer;
  664. opr.ref.options:=ref_selffixup;
  665. SetupSelf:=true;
  666. end
  667. else
  668. Message(asmr_e_cannot_use_SELF_outside_a_method);
  669. end;
  670. Function TOperand.SetupOldEBP:boolean;
  671. Begin
  672. SetupOldEBP:=false;
  673. if lexlevel>normal_function_level then
  674. Begin
  675. opr.typ:=OPR_REFERENCE;
  676. opr.ref.offset:=procinfo^.framepointer_offset;
  677. opr.ref.base:=procinfo^.framepointer;
  678. SetupOldEBP:=true;
  679. end
  680. else
  681. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  682. end;
  683. Function TOperand.SetupVar(const hs:string;GetOffset : boolean): Boolean;
  684. { search and sets up the correct fields in the Instr record }
  685. { for the NON-constant identifier passed to the routine. }
  686. { if not found returns FALSE. }
  687. var
  688. sym : psym;
  689. harrdef : parraydef;
  690. Begin
  691. SetupVar:=false;
  692. { are we in a routine ? }
  693. getsym(hs,false);
  694. sym:=srsym;
  695. if sym=nil then
  696. exit;
  697. case sym^.typ of
  698. varsym :
  699. begin
  700. { we always assume in asm statements that }
  701. { that the variable is valid. }
  702. pvarsym(sym)^.varstate:=vs_used;
  703. inc(pvarsym(sym)^.refs);
  704. case pvarsym(sym)^.owner^.symtabletype of
  705. objectsymtable :
  706. begin
  707. { this is not allowed, because we don't know if the self
  708. register is still free, and loading it first is also
  709. not possible, because this could break code }
  710. { Be TP/Delphi compatible in Delphi or TP modes }
  711. if (m_tp in aktmodeswitches) then
  712. begin
  713. opr.typ:=OPR_CONSTANT;
  714. opr.val:=pvarsym(sym)^.address;
  715. end
  716. { I do not agree here people using method vars should ensure
  717. that %esi is valid there }
  718. else
  719. begin
  720. opr.ref.base:=self_pointer;
  721. opr.ref.offset:=pvarsym(sym)^.address;
  722. end;
  723. hasvar:=true;
  724. SetupVar:=true;
  725. Exit;
  726. end;
  727. unitsymtable,
  728. globalsymtable,
  729. staticsymtable :
  730. opr.ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname);
  731. parasymtable :
  732. begin
  733. { if we only want the offset we don't have to care
  734. the base will be zeroed after ! }
  735. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) or
  736. { this below is wrong because there are two parast
  737. for global functions one of interface the second of
  738. implementation
  739. if (pvarsym(sym)^.owner=procinfo^.def^.parast) or }
  740. GetOffset then
  741. begin
  742. opr.ref.base:=procinfo^.framepointer;
  743. end
  744. else
  745. begin
  746. if (procinfo^.framepointer=R_ESP) and
  747. assigned(procinfo^.parent) and
  748. (lexlevel=pvarsym(sym)^.owner^.symtablelevel+1) and
  749. { same problem as above !!
  750. (procinfo^.parent^.sym^.definition^.parast=pvarsym(sym)^.owner) and }
  751. (lexlevel>normal_function_level) then
  752. opr.ref.base:=procinfo^.parent^.framepointer
  753. else
  754. message1(asmr_e_local_para_unreachable,hs);
  755. end;
  756. opr.ref.offset:=pvarsym(sym)^.address;
  757. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) then
  758. begin
  759. opr.ref.offsetfixup:=aktprocsym^.definition^.parast^.address_fixup;
  760. opr.ref.options:=ref_parafixup;
  761. end
  762. else
  763. begin
  764. opr.ref.offsetfixup:=0;
  765. opr.ref.options:=ref_none;
  766. end;
  767. if (pvarsym(sym)^.varspez=vs_var) or
  768. ((pvarsym(sym)^.varspez=vs_const) and
  769. push_addr_param(pvarsym(sym)^.vartype.def)) then
  770. SetSize(target_os.size_of_pointer,false);
  771. end;
  772. localsymtable :
  773. begin
  774. if (vo_is_external in pvarsym(sym)^.varoptions) then
  775. opr.ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname)
  776. else
  777. begin
  778. { if we only want the offset we don't have to care
  779. the base will be zeroed after ! }
  780. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) or
  781. {if (pvarsym(sym)^.owner=procinfo^.def^.localst) or}
  782. GetOffset then
  783. opr.ref.base:=procinfo^.framepointer
  784. else
  785. begin
  786. if (procinfo^.framepointer=R_ESP) and
  787. assigned(procinfo^.parent) and
  788. (lexlevel=pvarsym(sym)^.owner^.symtablelevel+1) and
  789. {(procinfo^.parent^.sym^.definition^.localst=pvarsym(sym)^.owner) and}
  790. (lexlevel>normal_function_level) then
  791. opr.ref.base:=procinfo^.parent^.framepointer
  792. else
  793. message1(asmr_e_local_para_unreachable,hs);
  794. end;
  795. opr.ref.offset:=-(pvarsym(sym)^.address);
  796. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) then
  797. begin
  798. opr.ref.offsetfixup:=aktprocsym^.definition^.localst^.address_fixup;
  799. opr.ref.options:=ref_localfixup;
  800. end
  801. else
  802. begin
  803. opr.ref.offsetfixup:=0;
  804. opr.ref.options:=ref_none;
  805. end;
  806. end;
  807. end;
  808. end;
  809. case pvarsym(sym)^.vartype.def^.deftype of
  810. orddef,
  811. enumdef,
  812. pointerdef,
  813. floatdef :
  814. SetSize(pvarsym(sym)^.getsize,false);
  815. arraydef :
  816. begin
  817. { for arrays try to get the element size, take care of
  818. multiple indexes }
  819. harrdef:=Parraydef(PVarsym(sym)^.vartype.def);
  820. while assigned(harrdef^.elementtype.def) and
  821. (harrdef^.elementtype.def^.deftype=arraydef) do
  822. harrdef:=parraydef(harrdef^.elementtype.def);
  823. SetSize(harrdef^.elesize,false);
  824. end;
  825. end;
  826. hasvar:=true;
  827. SetupVar:=true;
  828. Exit;
  829. end;
  830. typedconstsym :
  831. begin
  832. opr.ref.symbol:=newasmsymbol(ptypedconstsym(sym)^.mangledname);
  833. case ptypedconstsym(sym)^.typedconsttype.def^.deftype of
  834. orddef,
  835. enumdef,
  836. pointerdef,
  837. floatdef :
  838. SetSize(ptypedconstsym(sym)^.getsize,false);
  839. arraydef :
  840. begin
  841. { for arrays try to get the element size, take care of
  842. multiple indexes }
  843. harrdef:=Parraydef(PTypedConstSym(sym)^.typedconsttype.def);
  844. while assigned(harrdef^.elementtype.def) and
  845. (harrdef^.elementtype.def^.deftype=arraydef) do
  846. harrdef:=parraydef(harrdef^.elementtype.def);
  847. SetSize(harrdef^.elesize,false);
  848. end;
  849. end;
  850. hasvar:=true;
  851. SetupVar:=true;
  852. Exit;
  853. end;
  854. constsym :
  855. begin
  856. if pconstsym(sym)^.consttyp in [constint,constchar,constbool] then
  857. begin
  858. opr.typ:=OPR_CONSTANT;
  859. opr.val:=pconstsym(sym)^.value;
  860. SetupVar:=true;
  861. Exit;
  862. end;
  863. end;
  864. typesym :
  865. begin
  866. if ptypesym(sym)^.restype.def^.deftype in [recorddef,objectdef] then
  867. begin
  868. opr.typ:=OPR_CONSTANT;
  869. opr.val:=0;
  870. SetupVar:=TRUE;
  871. Exit;
  872. end;
  873. end;
  874. procsym :
  875. begin
  876. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  877. Message(asmr_w_calling_overload_func);
  878. opr.typ:=OPR_SYMBOL;
  879. opr.symbol:=newasmsymbol(pprocsym(sym)^.definition^.mangledname);
  880. hasvar:=true;
  881. SetupVar:=TRUE;
  882. Exit;
  883. end;
  884. else
  885. begin
  886. Message(asmr_e_unsupported_symbol_type);
  887. exit;
  888. end;
  889. end;
  890. end;
  891. { looks for internal names of variables and routines }
  892. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  893. {$ifndef OLDDIRECTVAR}
  894. var
  895. p : pasmsymbol;
  896. begin
  897. SetupDirectVar:=false;
  898. p:=getasmsymbol(hs);
  899. if assigned(p) then
  900. begin
  901. opr.ref.symbol:=p;
  902. hasvar:=true;
  903. SetupDirectVar:=true;
  904. end;
  905. end;
  906. {$else}
  907. var
  908. p : pai_external;
  909. Begin
  910. SearchDirectVar:=false;
  911. { search in the list of internals }
  912. p:=search_assembler_symbol(internals,hs,EXT_ANY);
  913. if p=nil then
  914. p:=search_assembler_symbol(externals,hs,EXT_ANY);
  915. if p<>nil then
  916. begin
  917. instr.operands[operandnum].opr.ref.symbol:=p^.sym;
  918. case p^.exttyp of
  919. EXT_BYTE : instr.operands[operandnum].size:=S_B;
  920. EXT_WORD : instr.operands[operandnum].size:=S_W;
  921. EXT_NEAR,EXT_FAR,EXT_PROC,EXT_DWORD,EXT_CODEPTR,EXT_DATAPTR:
  922. instr.operands[operandnum].size:=S_L;
  923. EXT_QWORD : instr.operands[operandnum].size:=S_FL;
  924. EXT_TBYTE : instr.operands[operandnum].size:=S_FX;
  925. else
  926. { this is in the case where the instruction is LEA }
  927. { or something like that, in that case size is not }
  928. { important. }
  929. instr.operands[operandnum].size:=S_NO;
  930. end;
  931. instr.operands[operandnum].hasvar:=true;
  932. SearchDirectVar:=TRUE;
  933. Exit;
  934. end;
  935. end;
  936. {$endif}
  937. procedure TOperand.InitRef;
  938. {*********************************************************************}
  939. { Description: This routine first check if the opcode is of }
  940. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  941. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  942. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  943. { to point to the default segment. }
  944. {*********************************************************************}
  945. Begin
  946. case opr.typ of
  947. OPR_REFERENCE:
  948. exit;
  949. OPR_NONE: ;
  950. else
  951. Message(asmr_e_invalid_operand_type);
  952. end;
  953. opr.typ := OPR_REFERENCE;
  954. reset_reference(opr.ref);
  955. end;
  956. procedure TOperand.BuildOperand;
  957. begin
  958. abstract;
  959. end;
  960. {****************************************************************************
  961. TInstruction
  962. ****************************************************************************}
  963. constructor TInstruction.init;
  964. Begin
  965. Opcode:=A_NONE;
  966. Opsize:=S_NO;
  967. Condition:=C_NONE;
  968. Ops:=0;
  969. InitOperands;
  970. end;
  971. destructor TInstruction.done;
  972. var
  973. i : longint;
  974. Begin
  975. for i:=1 to 3 do
  976. Dispose(Operands[i],Done);
  977. end;
  978. procedure TInstruction.InitOperands;
  979. var
  980. i : longint;
  981. begin
  982. for i:=1 to 3 do
  983. New(Operands[i],init);
  984. end;
  985. Procedure TInstruction.SwapOperands;
  986. Var
  987. p : POperand;
  988. Begin
  989. case Ops of
  990. 2 :
  991. begin
  992. p:=Operands[1];
  993. Operands[1]:=Operands[2];
  994. Operands[2]:=p;
  995. end;
  996. 3 :
  997. begin
  998. p:=Operands[1];
  999. Operands[1]:=Operands[3];
  1000. Operands[3]:=p;
  1001. end;
  1002. end;
  1003. end;
  1004. procedure TInstruction.ConcatInstruction(p:PAasmOutput);
  1005. begin
  1006. abstract;
  1007. end;
  1008. procedure TInstruction.BuildOpcode;
  1009. begin
  1010. abstract;
  1011. end;
  1012. {***************************************************************************
  1013. TLocalLabel
  1014. ***************************************************************************}
  1015. constructor TLocalLabel.Init(const n:string);
  1016. begin
  1017. inherited InitName(n);
  1018. lab:=nil;
  1019. emitted:=false;
  1020. end;
  1021. function TLocalLabel.Getpasmlabel:pasmlabel;
  1022. begin
  1023. if not assigned(lab) then
  1024. begin
  1025. getlabel(lab);
  1026. { this label is forced to be used so it's always written }
  1027. inc(lab^.refs);
  1028. end;
  1029. Getpasmlabel:=lab;
  1030. end;
  1031. {***************************************************************************
  1032. TLocalLabelList
  1033. ***************************************************************************}
  1034. procedure LocalLabelEmitted(p:PNamedIndexObject);{$ifndef FPC}far;{$endif}
  1035. begin
  1036. if not PLocalLabel(p)^.emitted then
  1037. Message1(asmr_e_unknown_label_identifier,p^.name);
  1038. end;
  1039. procedure TLocalLabelList.CheckEmitted;
  1040. begin
  1041. ForEach({$ifndef TP}@{$endif}LocalLabelEmitted)
  1042. end;
  1043. function CreateLocalLabel(const s: string; var hl: pasmlabel; emit:boolean):boolean;
  1044. var
  1045. lab : PLocalLabel;
  1046. Begin
  1047. CreateLocalLabel:=true;
  1048. { Check if it already is defined }
  1049. lab:=PLocalLabel(LocalLabelList^.Search(s));
  1050. if not assigned(lab) then
  1051. begin
  1052. new(lab,init(s));
  1053. LocalLabelList^.Insert(lab);
  1054. end;
  1055. { set emitted flag and check for dup syms }
  1056. if emit then
  1057. begin
  1058. if lab^.Emitted then
  1059. begin
  1060. Message1(asmr_e_dup_local_sym,lab^.Name);
  1061. CreateLocalLabel:=false;
  1062. end;
  1063. lab^.Emitted:=true;
  1064. end;
  1065. hl:=lab^.Getpasmlabel;
  1066. end;
  1067. {****************************************************************************
  1068. Symbol table helper routines
  1069. ****************************************************************************}
  1070. Function SearchType(const hs:string): Boolean;
  1071. begin
  1072. getsym(hs,false);
  1073. SearchType:=assigned(srsym) and
  1074. (srsym^.typ=typesym);
  1075. end;
  1076. Function SearchRecordType(const s:string): boolean;
  1077. Begin
  1078. SearchRecordType:=false;
  1079. { Check the constants in symtable }
  1080. getsym(s,false);
  1081. if srsym <> nil then
  1082. Begin
  1083. case srsym^.typ of
  1084. typesym :
  1085. begin
  1086. if ptypesym(srsym)^.restype.def^.deftype in [recorddef,objectdef] then
  1087. begin
  1088. SearchRecordType:=true;
  1089. exit;
  1090. end;
  1091. end;
  1092. end;
  1093. end;
  1094. end;
  1095. Function SearchIConstant(const s:string; var l:longint): boolean;
  1096. {**********************************************************************}
  1097. { Description: Searches for a CONSTANT of name s in either the local }
  1098. { symbol list, then in the global symbol list, and returns the value }
  1099. { of that constant in l. Returns TRUE if successfull, if not found, }
  1100. { or if the constant is not of correct type, then returns FALSE }
  1101. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1102. { respectively. }
  1103. {**********************************************************************}
  1104. Begin
  1105. SearchIConstant:=false;
  1106. { check for TRUE or FALSE reserved words first }
  1107. if s = 'TRUE' then
  1108. Begin
  1109. SearchIConstant:=TRUE;
  1110. l:=1;
  1111. exit;
  1112. end;
  1113. if s = 'FALSE' then
  1114. Begin
  1115. SearchIConstant:=TRUE;
  1116. l:=0;
  1117. exit;
  1118. end;
  1119. { Check the constants in symtable }
  1120. getsym(s,false);
  1121. if srsym <> nil then
  1122. Begin
  1123. case srsym^.typ of
  1124. constsym :
  1125. begin
  1126. if (pconstsym(srsym)^.consttyp in [constord,constint,constchar,constbool]) then
  1127. Begin
  1128. l:=pconstsym(srsym)^.value;
  1129. SearchIConstant:=TRUE;
  1130. exit;
  1131. end;
  1132. end;
  1133. enumsym:
  1134. Begin
  1135. l:=penumsym(srsym)^.value;
  1136. SearchIConstant:=TRUE;
  1137. exit;
  1138. end;
  1139. end;
  1140. end;
  1141. end;
  1142. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1143. { search and returns the offset and size of records/objects of the base }
  1144. { with field name setup in field. }
  1145. { returns FALSE if not found. }
  1146. { used when base is a variable or a typed constant name. }
  1147. var
  1148. st : psymtable;
  1149. harrdef : parraydef;
  1150. sym : psym;
  1151. i : longint;
  1152. base : string;
  1153. Begin
  1154. GetRecordOffsetSize:=FALSE;
  1155. Offset:=0;
  1156. Size:=0;
  1157. i:=pos('.',s);
  1158. if i=0 then
  1159. i:=255;
  1160. base:=Copy(s,1,i-1);
  1161. delete(s,1,i);
  1162. if base='SELF' then
  1163. st:=procinfo^._class^.symtable
  1164. else
  1165. begin
  1166. getsym(base,false);
  1167. sym:=srsym;
  1168. st:=nil;
  1169. { we can start with a var,type,typedconst }
  1170. case sym^.typ of
  1171. varsym :
  1172. begin
  1173. case pvarsym(sym)^.vartype.def^.deftype of
  1174. recorddef :
  1175. st:=precorddef(pvarsym(sym)^.vartype.def)^.symtable;
  1176. objectdef :
  1177. st:=pobjectdef(pvarsym(sym)^.vartype.def)^.symtable;
  1178. end;
  1179. end;
  1180. typesym :
  1181. begin
  1182. case ptypesym(sym)^.restype.def^.deftype of
  1183. recorddef :
  1184. st:=precorddef(ptypesym(sym)^.restype.def)^.symtable;
  1185. objectdef :
  1186. st:=pobjectdef(ptypesym(sym)^.restype.def)^.symtable;
  1187. end;
  1188. end;
  1189. typedconstsym :
  1190. begin
  1191. case ptypedconstsym(sym)^.typedconsttype.def^.deftype of
  1192. recorddef :
  1193. st:=precorddef(ptypedconstsym(sym)^.typedconsttype.def)^.symtable;
  1194. objectdef :
  1195. st:=pobjectdef(ptypedconstsym(sym)^.typedconsttype.def)^.symtable;
  1196. end;
  1197. end;
  1198. end;
  1199. end;
  1200. { now walk all recordsymtables }
  1201. while assigned(st) and (s<>'') do
  1202. begin
  1203. { load next field in base }
  1204. i:=pos('.',s);
  1205. if i=0 then
  1206. i:=255;
  1207. base:=Copy(s,1,i-1);
  1208. delete(s,1,i);
  1209. if st^.symtabletype=objectsymtable then
  1210. sym:=search_class_member(pobjectdef(st^.defowner),base)
  1211. else
  1212. sym:=st^.search(base);
  1213. if not assigned(sym) then
  1214. begin
  1215. GetRecordOffsetSize:=false;
  1216. exit;
  1217. end;
  1218. st:=nil;
  1219. case sym^.typ of
  1220. varsym :
  1221. begin
  1222. inc(Offset,pvarsym(sym)^.address);
  1223. Size:=PVarsym(sym)^.getsize;
  1224. case pvarsym(sym)^.vartype.def^.deftype of
  1225. arraydef :
  1226. begin
  1227. { for arrays try to get the element size, take care of
  1228. multiple indexes }
  1229. harrdef:=Parraydef(PVarsym(sym)^.vartype.def);
  1230. while assigned(harrdef^.elementtype.def) and
  1231. (harrdef^.elementtype.def^.deftype=arraydef) do
  1232. harrdef:=parraydef(harrdef^.elementtype.def);
  1233. size:=harrdef^.elesize;
  1234. end;
  1235. recorddef :
  1236. st:=precorddef(pvarsym(sym)^.vartype.def)^.symtable;
  1237. objectdef :
  1238. st:=pobjectdef(pvarsym(sym)^.vartype.def)^.symtable;
  1239. end;
  1240. end;
  1241. end;
  1242. end;
  1243. GetRecordOffsetSize:=(s='');
  1244. end;
  1245. Function SearchLabel(const s: string; var hl: pasmlabel;emit:boolean): boolean;
  1246. var
  1247. sym : psym;
  1248. hs : string;
  1249. Begin
  1250. hl:=nil;
  1251. SearchLabel:=false;
  1252. { Check for pascal labels, which are case insensetive }
  1253. hs:=upper(s);
  1254. getsym(hs,false);
  1255. sym:=srsym;
  1256. if sym=nil then
  1257. exit;
  1258. case sym^.typ of
  1259. labelsym :
  1260. begin
  1261. hl:=plabelsym(sym)^.lab;
  1262. if emit then
  1263. plabelsym(sym)^.defined:=true
  1264. else
  1265. plabelsym(sym)^.used:=true;
  1266. SearchLabel:=true;
  1267. exit;
  1268. end;
  1269. end;
  1270. end;
  1271. {*************************************************************************}
  1272. { Instruction Generation Utilities }
  1273. {*************************************************************************}
  1274. Procedure ConcatString(p : paasmoutput;s:string);
  1275. {*********************************************************************}
  1276. { PROCEDURE ConcatString(s:string); }
  1277. { Description: This routine adds the character chain pointed to in }
  1278. { s to the instruction linked list. }
  1279. {*********************************************************************}
  1280. Var
  1281. pc: PChar;
  1282. Begin
  1283. getmem(pc,length(s)+1);
  1284. p^.concat(new(pai_string,init_length_pchar(strpcopy(pc,s),length(s))));
  1285. end;
  1286. Procedure ConcatPasString(p : paasmoutput;s:string);
  1287. {*********************************************************************}
  1288. { PROCEDURE ConcatPasString(s:string); }
  1289. { Description: This routine adds the character chain pointed to in }
  1290. { s to the instruction linked list, contrary to ConcatString it }
  1291. { uses a pascal style string, so it conserves null characters. }
  1292. {*********************************************************************}
  1293. Begin
  1294. p^.concat(new(pai_string,init(s)));
  1295. end;
  1296. Procedure ConcatDirect(p : paasmoutput;s:string);
  1297. {*********************************************************************}
  1298. { PROCEDURE ConcatDirect(s:string) }
  1299. { Description: This routine output the string directly to the asm }
  1300. { output, it is only sed when writing special labels in AT&T mode, }
  1301. { and should not be used without due consideration, since it may }
  1302. { cause problems. }
  1303. {*********************************************************************}
  1304. Var
  1305. pc: PChar;
  1306. Begin
  1307. getmem(pc,length(s)+1);
  1308. p^.concat(new(pai_direct,init(strpcopy(pc,s))));
  1309. end;
  1310. Procedure ConcatConstant(p: paasmoutput; value: longint; maxvalue: longint);
  1311. {*********************************************************************}
  1312. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1313. { Description: This routine adds the value constant to the current }
  1314. { instruction linked list. }
  1315. { maxvalue -> indicates the size of the data to initialize: }
  1316. { $ff -> create a byte node. }
  1317. { $ffff -> create a word node. }
  1318. { $ffffffff -> create a dword node. }
  1319. {*********************************************************************}
  1320. Begin
  1321. if (maxvalue <> longint($ffffffff)) and (value > maxvalue) then
  1322. Begin
  1323. Message(asmr_e_constant_out_of_bounds);
  1324. { assuming a value of maxvalue }
  1325. value:=maxvalue;
  1326. end;
  1327. if maxvalue = $ff then
  1328. p^.concat(new(pai_const,init_8bit(byte(value))))
  1329. else
  1330. if maxvalue = $ffff then
  1331. p^.concat(new(pai_const,init_16bit(word(value))))
  1332. else
  1333. if maxvalue = longint($ffffffff) then
  1334. p^.concat(new(pai_const,init_32bit(longint(value))));
  1335. end;
  1336. Procedure ConcatConstSymbol(p : paasmoutput;const sym:string;l:longint);
  1337. begin
  1338. p^.concat(new(pai_const_symbol,initname_offset(sym,l)));
  1339. end;
  1340. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  1341. {***********************************************************************}
  1342. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1343. { Description: This routine adds the value constant to the current }
  1344. { instruction linked list. }
  1345. { real_typ -> indicates the type of the real data to initialize: }
  1346. { s32real -> create a single node. }
  1347. { s64real -> create a double node. }
  1348. { s80real -> create an extended node. }
  1349. { s64bit -> create a comp node. }
  1350. { f32bit -> create a fixed node. (not used normally) }
  1351. {***********************************************************************}
  1352. Begin
  1353. case real_typ of
  1354. s32real : p^.concat(new(pai_real_32bit,init(value)));
  1355. s64real : p^.concat(new(pai_real_64bit,init(value)));
  1356. s80real : p^.concat(new(pai_real_80bit,init(value)));
  1357. s64comp : p^.concat(new(pai_comp_64bit,init(value)));
  1358. f32bit : p^.concat(new(pai_const,init_32bit(trunc(value*$10000))));
  1359. end;
  1360. end;
  1361. Procedure ConcatLabel(p: paasmoutput;var l : pasmlabel);
  1362. {*********************************************************************}
  1363. { PROCEDURE ConcatLabel }
  1364. { Description: This routine either emits a label or a labeled }
  1365. { instruction to the linked list of instructions. }
  1366. {*********************************************************************}
  1367. begin
  1368. p^.concat(new(pai_label,init(l)));
  1369. end;
  1370. procedure ConcatAlign(p:paasmoutput;l:longint);
  1371. {*********************************************************************}
  1372. { PROCEDURE ConcatPublic }
  1373. { Description: This routine emits an global definition to the }
  1374. { linked list of instructions.(used by AT&T styled asm) }
  1375. {*********************************************************************}
  1376. begin
  1377. p^.concat(new(pai_align,init(l)));
  1378. end;
  1379. procedure ConcatPublic(p:paasmoutput;const s : string);
  1380. {*********************************************************************}
  1381. { PROCEDURE ConcatPublic }
  1382. { Description: This routine emits an global definition to the }
  1383. { linked list of instructions.(used by AT&T styled asm) }
  1384. {*********************************************************************}
  1385. begin
  1386. p^.concat(new(pai_symbol,initname_global(s,0)));
  1387. end;
  1388. procedure ConcatLocal(p:paasmoutput;const s : string);
  1389. {*********************************************************************}
  1390. { PROCEDURE ConcatLocal }
  1391. { Description: This routine emits an local definition to the }
  1392. { linked list of instructions. }
  1393. {*********************************************************************}
  1394. begin
  1395. p^.concat(new(pai_symbol,initname(s,0)));
  1396. end;
  1397. Procedure ConcatGlobalBss(const s : string;size : longint);
  1398. {*********************************************************************}
  1399. { PROCEDURE ConcatGlobalBss }
  1400. { Description: This routine emits an global datablock to the }
  1401. { linked list of instructions. }
  1402. {*********************************************************************}
  1403. begin
  1404. bsssegment^.concat(new(pai_datablock,init_global(s,size)));
  1405. end;
  1406. Procedure ConcatLocalBss(const s : string;size : longint);
  1407. {*********************************************************************}
  1408. { PROCEDURE ConcatLocalBss }
  1409. { Description: This routine emits a local datablcok to the }
  1410. { linked list of instructions. }
  1411. {*********************************************************************}
  1412. begin
  1413. bsssegment^.concat(new(pai_datablock,init(s,size)));
  1414. end;
  1415. end.
  1416. {
  1417. $Log$
  1418. Revision 1.3 2000-08-06 10:42:29 peter
  1419. * merged patches name generation in lib and asm constant eval
  1420. Revision 1.2 2000/07/13 11:32:48 michael
  1421. + removed logs
  1422. }