rautils.pas 45 KB

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