rautils.pas 47 KB

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