rautils.pas 47 KB

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