rautils.pas 46 KB

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