rautils.pas 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. This unit implements some support routines for assembler parsing
  4. independent of the processor
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. **********************************************************************}
  17. Unit RAUtils;
  18. {$i fpcdefs.inc}
  19. Interface
  20. Uses
  21. cutils,cclasses,
  22. globtype,aasmbase,aasmtai,aasmdata,cpubase,cpuinfo,cgbase,cgutils,
  23. symconst,symbase,symtype,symdef,symsym;
  24. Const
  25. RPNMax = 10; { I think you only need 4, but just to be safe }
  26. OpMax = 25;
  27. {---------------------------------------------------------------------
  28. Local Label Management
  29. ---------------------------------------------------------------------}
  30. Type
  31. { Each local label has this structure associated with it }
  32. TLocalLabel = class(TNamedIndexItem)
  33. Emitted : boolean;
  34. constructor Create(const n:string);
  35. function Gettasmlabel:tasmlabel;
  36. private
  37. lab : tasmlabel;
  38. end;
  39. TLocalLabelList = class(TDictionary)
  40. procedure CheckEmitted;
  41. end;
  42. var
  43. LocalLabelList : TLocalLabelList;
  44. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  45. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  46. {---------------------------------------------------------------------
  47. Instruction management
  48. ---------------------------------------------------------------------}
  49. type
  50. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_LOCAL,
  51. OPR_REFERENCE,OPR_REGISTER,OPR_REGLIST,OPR_COND,OPR_REGSET,OPR_SHIFTEROP);
  52. TOprRec = record
  53. case typ:TOprType of
  54. OPR_NONE : ();
  55. OPR_CONSTANT : (val:aint);
  56. OPR_SYMBOL : (symbol:tasmsymbol;symofs:aint);
  57. OPR_REFERENCE : (ref:treference);
  58. OPR_LOCAL : (localsym:tabstractnormalvarsym;localsymofs:aint;localindexreg:tregister;localscale:byte;localgetoffset,localforceref:boolean);
  59. OPR_REGISTER : (reg:tregister);
  60. {$ifdef m68k}
  61. OPR_REGLIST : (regset : tcpuregisterset);
  62. {$endif m68k}
  63. {$ifdef powerpc}
  64. OPR_COND : (cond : tasmcond);
  65. {$endif powerpc}
  66. {$ifdef POWERPC64}
  67. OPR_COND : (cond : tasmcond);
  68. {$endif POWERPC64}
  69. {$ifdef arm}
  70. OPR_REGSET : (regset : tcpuregisterset);
  71. OPR_SHIFTEROP : (shifterop : tshifterop);
  72. {$endif arm}
  73. end;
  74. TOperand = class
  75. typesize : aint;
  76. hastype, { if the operand has typecasted variable }
  77. hasvar : boolean; { if the operand is loaded with a variable }
  78. size : TCGSize;
  79. opr : TOprRec;
  80. constructor create;virtual;
  81. destructor destroy;override;
  82. Procedure SetSize(_size:longint;force:boolean);virtual;
  83. Procedure SetCorrectSize(opcode:tasmop);virtual;
  84. Function SetupResult:boolean;virtual;
  85. Function SetupSelf:boolean;
  86. Function SetupOldEBP:boolean;
  87. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  88. Procedure InitRef;
  89. end;
  90. TCOperand = class of TOperand;
  91. TInstruction = class
  92. opcode : tasmop;
  93. condition : tasmcond;
  94. ops : byte;
  95. labeled : boolean;
  96. operands : array[1..max_operands] of toperand;
  97. constructor create(optype : tcoperand);virtual;
  98. destructor destroy;override;
  99. { converts the instruction to an instruction how it's used by the assembler writer
  100. and concats it to the passed list, the newly created item is returned }
  101. function ConcatInstruction(p:TAsmList) : tai;virtual;
  102. Procedure Swapoperands;
  103. end;
  104. tstr2opentry = class(Tnamedindexitem)
  105. op: TAsmOp;
  106. end;
  107. {---------------------------------------------------------------------}
  108. { Expression parser types }
  109. {---------------------------------------------------------------------}
  110. TExprOperator = record
  111. ch: char; { operator }
  112. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  113. end;
  114. {**********************************************************************}
  115. { The following operators are supported: }
  116. { '+' : addition }
  117. { '-' : subtraction }
  118. { '*' : multiplication }
  119. { '/' : modulo division }
  120. { '^' : exclusive or }
  121. { '<' : shift left }
  122. { '>' : shift right }
  123. { '&' : bitwise and }
  124. { '|' : bitwise or }
  125. { '~' : bitwise complement }
  126. { '%' : modulo division }
  127. { nnn: longint numbers }
  128. { ( and ) parenthesis }
  129. {**********************************************************************}
  130. TExprParse = class
  131. public
  132. Constructor create;
  133. Destructor Destroy;override;
  134. Function Evaluate(Expr: String): aint;
  135. Function Priority(_Operator: Char): aint;
  136. private
  137. RPNStack : Array[1..RPNMax] of aint; { Stack For RPN calculator }
  138. RPNTop : aint;
  139. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  140. OpTop : aint;
  141. Procedure RPNPush(Num: aint);
  142. Function RPNPop: aint;
  143. Procedure RPNCalc(const token: String; prefix: boolean);
  144. Procedure OpPush(_Operator: char; prefix: boolean);
  145. { In reality returns TExprOperaotr }
  146. Procedure OpPop(var _Operator:TExprOperator);
  147. end;
  148. { Evaluate an expression string to a aint }
  149. Function CalculateExpression(const expression: string): aint;
  150. {---------------------------------------------------------------------}
  151. { String routines }
  152. {---------------------------------------------------------------------}
  153. Function ParseVal(const S:String;base:byte):aint;
  154. Function PadZero(Var s: String; n: byte): Boolean;
  155. Function EscapeToPascal(const s:string): string;
  156. {---------------------------------------------------------------------
  157. Symbol helper routines
  158. ---------------------------------------------------------------------}
  159. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  160. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint):boolean;
  161. Function SearchType(const hs:string;var size:aint): Boolean;
  162. Function SearchRecordType(const s:string): boolean;
  163. Function SearchIConstant(const s:string; var l:aint): boolean;
  164. {---------------------------------------------------------------------
  165. Instruction generation routines
  166. ---------------------------------------------------------------------}
  167. Procedure ConcatPasString(p : TAsmList;s:string);
  168. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  169. Procedure ConcatConstant(p : TAsmList;value: aint; constsize:byte);
  170. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:aint);
  171. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  172. Procedure ConcatString(p : TAsmList;s:string);
  173. procedure ConcatAlign(p:TAsmList;l:aint);
  174. Procedure ConcatPublic(p:TAsmList;const s : string);
  175. Procedure ConcatLocal(p:TAsmList;const s : string);
  176. Implementation
  177. uses
  178. strings,
  179. defutil,systems,verbose,globals,
  180. symtable,paramgr,
  181. aasmcpu,
  182. procinfo;
  183. {*************************************************************************
  184. TExprParse
  185. *************************************************************************}
  186. Constructor TExprParse.create;
  187. Begin
  188. end;
  189. Procedure TExprParse.RPNPush(Num : aint);
  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 : aint;
  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(const Token : String; prefix:boolean); { RPN Calculator }
  212. Var
  213. Temp : aint;
  214. n1,n2 : aint;
  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) : aint;
  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):aint;
  343. Var
  344. I : longint;
  345. Token : String;
  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 (OpTop>0) and (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.Destroy;
  429. Begin
  430. end;
  431. Function CalculateExpression(const expression: string): aint;
  432. var
  433. expr: TExprParse;
  434. Begin
  435. expr:=TExprParse.create;
  436. CalculateExpression:=expr.Evaluate(expression);
  437. expr.Free;
  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 : aint;
  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(ParseVal(temp,8));
  482. end;
  483. 'x':
  484. Begin
  485. temp:=s[i+1];
  486. temp:=temp+s[i+2];
  487. inc(i,2);
  488. c:=chr(ParseVal(temp,16));
  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 ParseVal(const S:String;base:byte):aint;
  506. { Converts a decimal string to aint }
  507. var
  508. code : integer;
  509. errmsg : word;
  510. prefix : string[2];
  511. Begin
  512. case base of
  513. 2 :
  514. begin
  515. errmsg:=asmr_e_error_converting_binary;
  516. prefix:='%';
  517. end;
  518. 8 :
  519. begin
  520. errmsg:=asmr_e_error_converting_octal;
  521. prefix:='&';
  522. end;
  523. 10 :
  524. begin
  525. errmsg:=asmr_e_error_converting_decimal;
  526. prefix:='';
  527. end;
  528. 16 :
  529. begin
  530. errmsg:=asmr_e_error_converting_hexadecimal;
  531. prefix:='$';
  532. end;
  533. else
  534. internalerror(200501202);
  535. end;
  536. val(prefix+s,result,code);
  537. if code<>0 then
  538. begin
  539. val(prefix+s,aword(result),code);
  540. if code<>0 then
  541. begin
  542. Message1(errmsg,s);
  543. result:=0;
  544. end;
  545. end;
  546. end;
  547. Function PadZero(Var s: String; n: byte): Boolean;
  548. Begin
  549. PadZero:=TRUE;
  550. { Do some error checking first }
  551. if Length(s) = n then
  552. exit
  553. else
  554. if Length(s) > n then
  555. Begin
  556. PadZero:=FALSE;
  557. delete(s,n+1,length(s));
  558. exit;
  559. end
  560. else
  561. PadZero:=TRUE;
  562. { Fill it up with the specified character }
  563. fillchar(s[length(s)+1],n-1,#0);
  564. s[0]:=chr(n);
  565. end;
  566. {****************************************************************************
  567. TOperand
  568. ****************************************************************************}
  569. constructor TOperand.Create;
  570. begin
  571. size:=OS_NO;
  572. hastype:=false;
  573. hasvar:=false;
  574. FillChar(Opr,sizeof(Opr),0);
  575. end;
  576. destructor TOperand.destroy;
  577. begin
  578. end;
  579. Procedure TOperand.SetSize(_size:longint;force:boolean);
  580. begin
  581. if force or
  582. ((size = OS_NO) and (_size<=16)) then
  583. Begin
  584. case _size of
  585. 1 : size:=OS_8;
  586. 2 : size:=OS_16{ could be S_IS};
  587. 4 : size:=OS_32{ could be S_IL or S_FS};
  588. 8 : size:=OS_64{ could be S_D or S_FL};
  589. 10 : size:=OS_F80;
  590. 16 : size:=OS_128;
  591. end;
  592. end;
  593. end;
  594. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  595. begin
  596. end;
  597. function TOperand.SetupResult:boolean;
  598. begin
  599. SetupResult:=false;
  600. { replace by correct offset. }
  601. with current_procinfo.procdef do
  602. if (not is_void(returndef)) then
  603. begin
  604. if (m_tp7 in aktmodeswitches) and
  605. (not paramanager.ret_in_param(returndef,proccalloption)) then
  606. begin
  607. message(asmr_e_cannot_use_RESULT_here);
  608. exit;
  609. end;
  610. SetupResult:=setupvar('result',false)
  611. end
  612. else
  613. message(asmr_e_void_function);
  614. end;
  615. Function TOperand.SetupSelf:boolean;
  616. Begin
  617. SetupSelf:=false;
  618. if assigned(current_procinfo.procdef._class) then
  619. SetupSelf:=setupvar('self',false)
  620. else
  621. Message(asmr_e_cannot_use_SELF_outside_a_method);
  622. end;
  623. Function TOperand.SetupOldEBP:boolean;
  624. Begin
  625. SetupOldEBP:=false;
  626. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  627. SetupOldEBP:=setupvar('parentframe',false)
  628. else
  629. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  630. end;
  631. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  632. function symtable_has_localvarsyms(st:tsymtable):boolean;
  633. var
  634. sym : tsym;
  635. begin
  636. result:=false;
  637. sym:=tsym(st.symindex.first);
  638. while assigned(sym) do
  639. begin
  640. if sym.typ=localvarsym then
  641. begin
  642. result:=true;
  643. exit;
  644. end;
  645. sym:=tsym(sym.indexnext);
  646. end;
  647. end;
  648. procedure setconst(l:aint);
  649. begin
  650. { We return the address of the field, just like Delphi/TP }
  651. case opr.typ of
  652. OPR_NONE :
  653. begin
  654. opr.typ:=OPR_CONSTANT;
  655. opr.val:=l;
  656. end;
  657. OPR_CONSTANT :
  658. inc(opr.val,l);
  659. OPR_REFERENCE :
  660. inc(opr.ref.offset,l);
  661. OPR_LOCAL :
  662. inc(opr.localsymofs,l);
  663. else
  664. Message(asmr_e_invalid_operand_type);
  665. end;
  666. end;
  667. { search and sets up the correct fields in the Instr record }
  668. { for the NON-constant identifier passed to the routine. }
  669. { if not found returns FALSE. }
  670. var
  671. sym : tsym;
  672. srsymtable : tsymtable;
  673. harrdef : tarraydef;
  674. indexreg : tregister;
  675. l : aint;
  676. plist : ppropaccesslistitem;
  677. Begin
  678. SetupVar:=false;
  679. asmsearchsym(s,sym,srsymtable);
  680. if sym = nil then
  681. exit;
  682. if sym.typ=absolutevarsym then
  683. begin
  684. if (tabsolutevarsym(sym).abstyp=tovar) then
  685. begin
  686. { Only support simple loads }
  687. plist:=tabsolutevarsym(sym).ref.firstsym;
  688. if assigned(plist) and
  689. (plist^.sltype=sl_load) then
  690. sym:=plist^.sym
  691. else
  692. begin
  693. Message(asmr_e_unsupported_symbol_type);
  694. exit;
  695. end;
  696. end
  697. else
  698. begin
  699. Message(asmr_e_unsupported_symbol_type);
  700. exit;
  701. end;
  702. end;
  703. case sym.typ of
  704. fieldvarsym :
  705. begin
  706. setconst(tfieldvarsym(sym).fieldoffset);
  707. hasvar:=true;
  708. SetupVar:=true;
  709. end;
  710. globalvarsym,
  711. localvarsym,
  712. paravarsym :
  713. begin
  714. { we always assume in asm statements that }
  715. { that the variable is valid. }
  716. tabstractvarsym(sym).varstate:=vs_readwritten;
  717. inc(tabstractvarsym(sym).refs);
  718. { variable can't be placed in a register }
  719. tabstractvarsym(sym).varregable:=vr_none;
  720. case sym.owner.symtabletype of
  721. globalsymtable,
  722. staticsymtable :
  723. begin
  724. initref;
  725. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tglobalvarsym(sym).mangledname);
  726. end;
  727. parasymtable,
  728. localsymtable :
  729. begin
  730. if opr.typ=OPR_REFERENCE then
  731. begin
  732. indexreg:=opr.ref.base;
  733. if opr.ref.index<>NR_NO then
  734. begin
  735. if indexreg=NR_NO then
  736. indexreg:=opr.ref.index
  737. else
  738. Message(asmr_e_multiple_index);
  739. end;
  740. end
  741. else
  742. indexreg:=NR_NO;
  743. opr.typ:=OPR_LOCAL;
  744. if assigned(current_procinfo.parent) and
  745. not(po_inline in current_procinfo.procdef.procoptions) and
  746. (sym.owner<>current_procinfo.procdef.localst) and
  747. (sym.owner<>current_procinfo.procdef.parast) and
  748. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  749. symtable_has_localvarsyms(current_procinfo.procdef.localst) then
  750. message1(asmr_e_local_para_unreachable,s);
  751. opr.localsym:=tabstractnormalvarsym(sym);
  752. opr.localsymofs:=0;
  753. opr.localindexreg:=indexreg;
  754. opr.localscale:=0;
  755. opr.localgetoffset:=GetOffset;
  756. if paramanager.push_addr_param(tabstractvarsym(sym).varspez,tabstractvarsym(sym).vardef,current_procinfo.procdef.proccalloption) then
  757. SetSize(sizeof(aint),false);
  758. end;
  759. end;
  760. case tabstractvarsym(sym).vardef.deftype of
  761. orddef,
  762. enumdef,
  763. pointerdef,
  764. arraydef,
  765. floatdef :
  766. SetSize(tabstractvarsym(sym).getsize,false);
  767. (* makes no sense when using sse instructions (FK)
  768. arraydef :
  769. begin
  770. { for arrays try to get the element size, take care of
  771. multiple indexes }
  772. harrdef:=tarraydef(tabstractvarsym(sym).vardef);
  773. while assigned(harrdef.elementdef) and
  774. (harrdef.elementdef.deftype=arraydef) do
  775. harrdef:=tarraydef(harrdef.elementdef);
  776. SetSize(harrdef.elesize,false);
  777. end;
  778. *)
  779. end;
  780. hasvar:=true;
  781. SetupVar:=true;
  782. Exit;
  783. end;
  784. typedconstsym :
  785. begin
  786. initref;
  787. opr.ref.symbol:=current_asmdata.RefAsmSymbol(ttypedconstsym(sym).mangledname);
  788. case ttypedconstsym(sym).typedconstdef.deftype of
  789. orddef,
  790. enumdef,
  791. pointerdef,
  792. floatdef :
  793. SetSize(ttypedconstsym(sym).getsize,false);
  794. arraydef :
  795. begin
  796. { for arrays try to get the element size, take care of
  797. multiple indexes }
  798. harrdef:=tarraydef(ttypedconstsym(sym).typedconstdef);
  799. while assigned(harrdef.elementdef) and
  800. (harrdef.elementdef.deftype=arraydef) do
  801. harrdef:=tarraydef(harrdef.elementdef);
  802. if not is_packed_array(harrdef) then
  803. SetSize(harrdef.elesize,false)
  804. else
  805. begin
  806. if (harrdef.elepackedbitsize mod 8) <> 0 then
  807. Message(asmr_e_packed_element);
  808. SetSize((harrdef.elepackedbitsize + 7) div 8,false);
  809. end;
  810. end;
  811. end;
  812. hasvar:=true;
  813. SetupVar:=true;
  814. Exit;
  815. end;
  816. constsym :
  817. begin
  818. if tconstsym(sym).consttyp=constord then
  819. begin
  820. setconst(tconstsym(sym).value.valueord);
  821. SetupVar:=true;
  822. Exit;
  823. end;
  824. end;
  825. typesym :
  826. begin
  827. if ttypesym(sym).typedef.deftype in [recorddef,objectdef] then
  828. begin
  829. setconst(0);
  830. SetupVar:=TRUE;
  831. Exit;
  832. end;
  833. end;
  834. procsym :
  835. begin
  836. if opr.typ<>OPR_NONE then
  837. Message(asmr_e_invalid_operand_type);
  838. if Tprocsym(sym).procdef_count>1 then
  839. Message(asmr_w_calling_overload_func);
  840. l:=opr.ref.offset;
  841. opr.typ:=OPR_SYMBOL;
  842. opr.symbol:=current_asmdata.RefAsmSymbol(tprocsym(sym).first_procdef.mangledname);
  843. opr.symofs:=l;
  844. hasvar:=true;
  845. SetupVar:=TRUE;
  846. Exit;
  847. end;
  848. else
  849. begin
  850. Message(asmr_e_unsupported_symbol_type);
  851. exit;
  852. end;
  853. end;
  854. end;
  855. procedure TOperand.InitRef;
  856. {*********************************************************************}
  857. { Description: This routine first check if the opcode is of }
  858. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  859. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  860. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  861. { to point to the default segment. }
  862. {*********************************************************************}
  863. var
  864. l : aint;
  865. hsymofs : aint;
  866. hsymbol : tasmsymbol;
  867. reg : tregister;
  868. Begin
  869. case opr.typ of
  870. OPR_REFERENCE :
  871. exit;
  872. OPR_CONSTANT :
  873. begin
  874. l:=opr.val;
  875. opr.typ:=OPR_REFERENCE;
  876. Fillchar(opr.ref,sizeof(treference),0);
  877. opr.Ref.Offset:=l;
  878. end;
  879. OPR_NONE :
  880. begin
  881. opr.typ:=OPR_REFERENCE;
  882. Fillchar(opr.ref,sizeof(treference),0);
  883. end;
  884. OPR_REGISTER :
  885. begin
  886. reg:=opr.reg;
  887. opr.typ:=OPR_REFERENCE;
  888. Fillchar(opr.ref,sizeof(treference),0);
  889. opr.Ref.base:=reg;
  890. end;
  891. OPR_SYMBOL :
  892. begin
  893. hsymbol:=opr.symbol;
  894. hsymofs:=opr.symofs;
  895. opr.typ:=OPR_REFERENCE;
  896. Fillchar(opr.ref,sizeof(treference),0);
  897. opr.ref.symbol:=hsymbol;
  898. opr.ref.offset:=hsymofs;
  899. end;
  900. else
  901. begin
  902. Message(asmr_e_invalid_operand_type);
  903. { Recover }
  904. opr.typ:=OPR_REFERENCE;
  905. Fillchar(opr.ref,sizeof(treference),0);
  906. end;
  907. end;
  908. end;
  909. {****************************************************************************
  910. TInstruction
  911. ****************************************************************************}
  912. constructor TInstruction.create(optype : tcoperand);
  913. var
  914. i : longint;
  915. Begin
  916. { these field are set to 0 anyways by the constructor helper (FK)
  917. Opcode:=A_NONE;
  918. Condition:=C_NONE;
  919. Ops:=0;
  920. }
  921. for i:=1 to max_operands do
  922. Operands[i]:=optype.create;
  923. Labeled:=false;
  924. end;
  925. destructor TInstruction.destroy;
  926. var
  927. i : longint;
  928. Begin
  929. for i:=1 to max_operands do
  930. Operands[i].free;
  931. end;
  932. Procedure TInstruction.Swapoperands;
  933. Var
  934. p : toperand;
  935. Begin
  936. case Ops of
  937. 2 :
  938. begin
  939. p:=Operands[1];
  940. Operands[1]:=Operands[2];
  941. Operands[2]:=p;
  942. end;
  943. 3 :
  944. begin
  945. p:=Operands[1];
  946. Operands[1]:=Operands[3];
  947. Operands[3]:=p;
  948. end;
  949. end;
  950. end;
  951. function TInstruction.ConcatInstruction(p:TAsmList) : tai;
  952. var
  953. ai : taicpu;
  954. i : longint;
  955. begin
  956. ai:=taicpu.op_none(opcode);
  957. ai.Ops:=Ops;
  958. ai.Allocate_oper(Ops);
  959. for i:=1 to Ops do
  960. with operands[i].opr do
  961. begin
  962. case typ of
  963. OPR_CONSTANT :
  964. ai.loadconst(i-1,val);
  965. OPR_REGISTER:
  966. ai.loadreg(i-1,reg);
  967. OPR_SYMBOL:
  968. ai.loadsymbol(i-1,symbol,symofs);
  969. OPR_LOCAL :
  970. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  971. localscale,localgetoffset,localforceref);
  972. OPR_REFERENCE:
  973. ai.loadref(i-1,ref);
  974. {$ifdef ARM}
  975. OPR_REGSET:
  976. ai.loadregset(i-1,regset);
  977. OPR_SHIFTEROP:
  978. ai.loadshifterop(i-1,shifterop);
  979. {$endif ARM}
  980. else
  981. internalerror(200501051);
  982. end;
  983. end;
  984. ai.SetCondition(condition);
  985. { Concat the opcode or give an error }
  986. if assigned(ai) then
  987. p.concat(ai)
  988. else
  989. Message(asmr_e_invalid_opcode_and_operand);
  990. result:=ai;
  991. end;
  992. {***************************************************************************
  993. TLocalLabel
  994. ***************************************************************************}
  995. constructor TLocalLabel.create(const n:string);
  996. begin
  997. inherited CreateName(n);
  998. lab:=nil;
  999. emitted:=false;
  1000. end;
  1001. function TLocalLabel.Gettasmlabel:tasmlabel;
  1002. begin
  1003. if not assigned(lab) then
  1004. begin
  1005. current_asmdata.getjumplabel(lab);
  1006. { this label is forced to be used so it's always written }
  1007. lab.increfs;
  1008. end;
  1009. Gettasmlabel:=lab;
  1010. end;
  1011. {***************************************************************************
  1012. TLocalLabelList
  1013. ***************************************************************************}
  1014. procedure LocalLabelEmitted(p:tnamedindexitem;arg:pointer);
  1015. begin
  1016. if not TLocalLabel(p).emitted then
  1017. Message1(asmr_e_unknown_label_identifier,p.name);
  1018. end;
  1019. procedure TLocalLabelList.CheckEmitted;
  1020. begin
  1021. ForEach_Static(@LocalLabelEmitted,nil)
  1022. end;
  1023. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1024. var
  1025. lab : TLocalLabel;
  1026. Begin
  1027. CreateLocalLabel:=true;
  1028. { Check if it already is defined }
  1029. lab:=TLocalLabel(LocalLabellist.Search(s));
  1030. if not assigned(lab) then
  1031. begin
  1032. lab:=TLocalLabel.Create(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.Gettasmlabel;
  1046. end;
  1047. {****************************************************************************
  1048. Symbol table helper routines
  1049. ****************************************************************************}
  1050. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  1051. var
  1052. i : integer;
  1053. begin
  1054. i:=pos('.',s);
  1055. { allow unit.identifier }
  1056. if i>0 then
  1057. begin
  1058. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1059. if assigned(srsym) then
  1060. begin
  1061. if (srsym.typ=unitsym) and
  1062. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  1063. srsym.owner.iscurrentunit then
  1064. searchsym_in_module(tunitsym(srsym).module,Copy(s,i+1,255),srsym,srsymtable)
  1065. else
  1066. begin
  1067. srsym:=nil;
  1068. srsymtable:=nil;
  1069. end;
  1070. end;
  1071. end
  1072. else
  1073. searchsym(s,srsym,srsymtable);
  1074. end;
  1075. Function SearchType(const hs:string;var size:aint): Boolean;
  1076. var
  1077. srsym : tsym;
  1078. srsymtable : tsymtable;
  1079. begin
  1080. result:=false;
  1081. size:=0;
  1082. asmsearchsym(hs,srsym,srsymtable);
  1083. if assigned(srsym) and
  1084. (srsym.typ=typesym) then
  1085. begin
  1086. size:=ttypesym(srsym).typedef.size;
  1087. result:=true;
  1088. end;
  1089. end;
  1090. Function SearchRecordType(const s:string): boolean;
  1091. var
  1092. srsym : tsym;
  1093. srsymtable : tsymtable;
  1094. Begin
  1095. SearchRecordType:=false;
  1096. { Check the constants in symtable }
  1097. asmsearchsym(s,srsym,srsymtable);
  1098. if srsym <> nil then
  1099. Begin
  1100. case srsym.typ of
  1101. typesym :
  1102. begin
  1103. if ttypesym(srsym).typedef.deftype in [recorddef,objectdef] then
  1104. begin
  1105. SearchRecordType:=true;
  1106. exit;
  1107. end;
  1108. end;
  1109. end;
  1110. end;
  1111. end;
  1112. Function SearchIConstant(const s:string; var l:aint): boolean;
  1113. {**********************************************************************}
  1114. { Description: Searches for a CONSTANT of name s in either the local }
  1115. { symbol list, then in the global symbol list, and returns the value }
  1116. { of that constant in l. Returns TRUE if successfull, if not found, }
  1117. { or if the constant is not of correct type, then returns FALSE }
  1118. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1119. { respectively. }
  1120. {**********************************************************************}
  1121. var
  1122. srsym : tsym;
  1123. srsymtable : tsymtable;
  1124. Begin
  1125. SearchIConstant:=false;
  1126. { check for TRUE or FALSE reserved words first }
  1127. if s = 'TRUE' then
  1128. Begin
  1129. SearchIConstant:=TRUE;
  1130. l:=1;
  1131. exit;
  1132. end;
  1133. if s = 'FALSE' then
  1134. Begin
  1135. SearchIConstant:=TRUE;
  1136. l:=0;
  1137. exit;
  1138. end;
  1139. { Check the constants in symtable }
  1140. asmsearchsym(s,srsym,srsymtable);
  1141. if srsym <> nil then
  1142. Begin
  1143. case srsym.typ of
  1144. constsym :
  1145. begin
  1146. if tconstsym(srsym).consttyp=constord then
  1147. Begin
  1148. l:=tconstsym(srsym).value.valueord;
  1149. SearchIConstant:=TRUE;
  1150. exit;
  1151. end;
  1152. end;
  1153. enumsym:
  1154. Begin
  1155. l:=tenumsym(srsym).value;
  1156. SearchIConstant:=TRUE;
  1157. exit;
  1158. end;
  1159. end;
  1160. end;
  1161. end;
  1162. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint):boolean;
  1163. { search and returns the offset and size of records/objects of the base }
  1164. { with field name setup in field. }
  1165. { returns FALSE if not found. }
  1166. { used when base is a variable or a typed constant name. }
  1167. var
  1168. st : tsymtable;
  1169. harrdef : tarraydef;
  1170. sym : tsym;
  1171. srsymtable : tsymtable;
  1172. i : longint;
  1173. base : string;
  1174. Begin
  1175. GetRecordOffsetSize:=FALSE;
  1176. Offset:=0;
  1177. Size:=0;
  1178. i:=pos('.',s);
  1179. if i=0 then
  1180. i:=255;
  1181. base:=Copy(s,1,i-1);
  1182. delete(s,1,i);
  1183. if base='SELF' then
  1184. st:=current_procinfo.procdef._class.symtable
  1185. else
  1186. begin
  1187. asmsearchsym(base,sym,srsymtable);
  1188. st:=nil;
  1189. { we can start with a var,type,typedconst }
  1190. if assigned(sym) then
  1191. case sym.typ of
  1192. globalvarsym,
  1193. localvarsym,
  1194. paravarsym :
  1195. st:=Tabstractvarsym(sym).vardef.getsymtable(gs_record);
  1196. typesym :
  1197. st:=Ttypesym(sym).typedef.getsymtable(gs_record);
  1198. typedconstsym :
  1199. st:=Ttypedconstsym(sym).typedconstdef.getsymtable(gs_record);
  1200. end
  1201. else
  1202. s:='';
  1203. end;
  1204. { now walk all recordsymtables }
  1205. while assigned(st) and (s<>'') do
  1206. begin
  1207. { load next field in base }
  1208. i:=pos('.',s);
  1209. if i=0 then
  1210. i:=255;
  1211. base:=Copy(s,1,i-1);
  1212. delete(s,1,i);
  1213. if st.symtabletype=objectsymtable then
  1214. sym:=search_class_member(tobjectdef(st.defowner),base)
  1215. else
  1216. sym:=tsym(st.search(base));
  1217. if not assigned(sym) then
  1218. begin
  1219. GetRecordOffsetSize:=false;
  1220. exit;
  1221. end;
  1222. st:=nil;
  1223. case sym.typ of
  1224. fieldvarsym :
  1225. with Tfieldvarsym(sym) do
  1226. begin
  1227. inc(Offset,fieldoffset);
  1228. size:=getsize;
  1229. case vardef.deftype of
  1230. arraydef :
  1231. begin
  1232. { for arrays try to get the element size, take care of
  1233. multiple indexes }
  1234. harrdef:=tarraydef(vardef);
  1235. while assigned(harrdef.elementdef) and
  1236. (harrdef.elementdef.deftype=arraydef) do
  1237. harrdef:=tarraydef(harrdef.elementdef);
  1238. if not is_packed_array(harrdef) then
  1239. size:=harrdef.elesize
  1240. else
  1241. begin
  1242. if (harrdef.elepackedbitsize mod 8) <> 0 then
  1243. Message(asmr_e_packed_element);
  1244. size := (harrdef.elepackedbitsize + 7) div 8;
  1245. end;
  1246. end;
  1247. recorddef :
  1248. st:=trecorddef(vardef).symtable;
  1249. objectdef :
  1250. st:=tobjectdef(vardef).symtable;
  1251. end;
  1252. end;
  1253. end;
  1254. end;
  1255. { Support Field.Type as typecasting }
  1256. if (st=nil) and (s<>'') then
  1257. begin
  1258. asmsearchsym(s,sym,srsymtable);
  1259. if assigned(sym) and (sym.typ=typesym) then
  1260. begin
  1261. size:=ttypesym(sym).typedef.size;
  1262. s:=''
  1263. end;
  1264. end;
  1265. GetRecordOffsetSize:=(s='');
  1266. end;
  1267. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1268. var
  1269. sym : tsym;
  1270. srsymtable : tsymtable;
  1271. hs : string;
  1272. Begin
  1273. hl:=nil;
  1274. SearchLabel:=false;
  1275. { Check for pascal labels, which are case insensetive }
  1276. hs:=upper(s);
  1277. asmsearchsym(hs,sym,srsymtable);
  1278. if sym=nil then
  1279. exit;
  1280. case sym.typ of
  1281. labelsym :
  1282. begin
  1283. if not(assigned(tlabelsym(sym).asmblocklabel)) then
  1284. current_asmdata.getjumplabel(tlabelsym(sym).asmblocklabel);
  1285. hl:=tlabelsym(sym).asmblocklabel;
  1286. if emit then
  1287. tlabelsym(sym).defined:=true
  1288. else
  1289. tlabelsym(sym).used:=true;
  1290. SearchLabel:=true;
  1291. end;
  1292. end;
  1293. end;
  1294. {*************************************************************************}
  1295. { Instruction Generation Utilities }
  1296. {*************************************************************************}
  1297. Procedure ConcatString(p : TAsmList;s:string);
  1298. {*********************************************************************}
  1299. { PROCEDURE ConcatString(s:string); }
  1300. { Description: This routine adds the character chain pointed to in }
  1301. { s to the instruction linked list. }
  1302. {*********************************************************************}
  1303. Var
  1304. pc: PChar;
  1305. Begin
  1306. getmem(pc,length(s)+1);
  1307. p.concat(Tai_string.Create_pchar(strpcopy(pc,s),length(s)));
  1308. end;
  1309. Procedure ConcatPasString(p : TAsmList;s:string);
  1310. {*********************************************************************}
  1311. { PROCEDURE ConcatPasString(s:string); }
  1312. { Description: This routine adds the character chain pointed to in }
  1313. { s to the instruction linked list, contrary to ConcatString it }
  1314. { uses a pascal style string, so it conserves null characters. }
  1315. {*********************************************************************}
  1316. Begin
  1317. p.concat(Tai_string.Create(s));
  1318. end;
  1319. Procedure ConcatConstant(p: TAsmList; value: aint; constsize:byte);
  1320. {*********************************************************************}
  1321. { PROCEDURE ConcatConstant(value: aint; maxvalue: aint); }
  1322. { Description: This routine adds the value constant to the current }
  1323. { instruction linked list. }
  1324. { maxvalue -> indicates the size of the data to initialize: }
  1325. { $ff -> create a byte node. }
  1326. { $ffff -> create a word node. }
  1327. { $ffffffff -> create a dword node. }
  1328. {*********************************************************************}
  1329. var
  1330. rangelo,rangehi : int64;
  1331. Begin
  1332. case constsize of
  1333. 1 :
  1334. begin
  1335. p.concat(Tai_const.Create_8bit(byte(value)));
  1336. rangelo:=low(shortint);
  1337. rangehi:=high(byte);
  1338. end;
  1339. 2 :
  1340. begin
  1341. p.concat(Tai_const.Create_16bit(word(value)));
  1342. rangelo:=low(smallint);
  1343. rangehi:=high(word);
  1344. end;
  1345. 4 :
  1346. begin
  1347. p.concat(Tai_const.Create_32bit(longint(value)));
  1348. rangelo:=low(longint);
  1349. rangehi:=high(cardinal);
  1350. end;
  1351. 8 :
  1352. begin
  1353. p.concat(Tai_const.Create_64bit(int64(value)));
  1354. rangelo:=0;
  1355. rangehi:=0;
  1356. end;
  1357. else
  1358. internalerror(200405011);
  1359. end;
  1360. { check for out of bounds }
  1361. if (rangelo<>0) and
  1362. ((value>rangehi) or (value<rangelo)) then
  1363. Message(asmr_e_constant_out_of_bounds);
  1364. end;
  1365. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:aint);
  1366. begin
  1367. p.concat(Tai_const.Createname(sym,l));
  1368. end;
  1369. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  1370. {***********************************************************************}
  1371. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1372. { Description: This routine adds the value constant to the current }
  1373. { instruction linked list. }
  1374. { real_typ -> indicates the type of the real data to initialize: }
  1375. { s32real -> create a single node. }
  1376. { s64real -> create a double node. }
  1377. { s80real -> create an extended node. }
  1378. { s64bit -> create a comp node. }
  1379. { f32bit -> create a fixed node. (not used normally) }
  1380. {***********************************************************************}
  1381. Begin
  1382. case real_typ of
  1383. s32real : p.concat(Tai_real_32bit.Create(value));
  1384. s64real :
  1385. {$ifdef ARM}
  1386. if aktfputype in [fpu_fpa,fpu_fpa10,fpu_fpa11] then
  1387. p.concat(Tai_real_64bit.Create_hiloswapped(value))
  1388. else
  1389. {$endif ARM}
  1390. p.concat(Tai_real_64bit.Create(value));
  1391. s80real : p.concat(Tai_real_80bit.Create(value));
  1392. s64comp : p.concat(Tai_comp_64bit.Create(trunc(value)));
  1393. end;
  1394. end;
  1395. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  1396. {*********************************************************************}
  1397. { PROCEDURE ConcatLabel }
  1398. { Description: This routine either emits a label or a labeled }
  1399. { instruction to the linked list of instructions. }
  1400. {*********************************************************************}
  1401. begin
  1402. p.concat(Tai_label.Create(l));
  1403. end;
  1404. procedure ConcatAlign(p:TAsmList;l:aint);
  1405. {*********************************************************************}
  1406. { PROCEDURE ConcatPublic }
  1407. { Description: This routine emits an global definition to the }
  1408. { linked list of instructions.(used by AT&T styled asm) }
  1409. {*********************************************************************}
  1410. begin
  1411. p.concat(Tai_align.Create(l));
  1412. end;
  1413. procedure ConcatPublic(p:TAsmList;const s : string);
  1414. {*********************************************************************}
  1415. { PROCEDURE ConcatPublic }
  1416. { Description: This routine emits an global definition to the }
  1417. { linked list of instructions.(used by AT&T styled asm) }
  1418. {*********************************************************************}
  1419. begin
  1420. p.concat(Tai_symbol.Createname_global(s,AT_LABEL,0));
  1421. end;
  1422. procedure ConcatLocal(p:TAsmList;const s : string);
  1423. {*********************************************************************}
  1424. { PROCEDURE ConcatLocal }
  1425. { Description: This routine emits an local definition to the }
  1426. { linked list of instructions. }
  1427. {*********************************************************************}
  1428. begin
  1429. p.concat(Tai_symbol.Createname(s,AT_LABEL,0));
  1430. end;
  1431. end.