rautils.pas 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596
  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(TFPHashObject)
  33. Emitted : boolean;
  34. constructor Create(AList:TFPHashObjectList;const n:string);
  35. function Gettasmlabel:tasmlabel;
  36. private
  37. lab : tasmlabel;
  38. end;
  39. TLocalLabelList = class(TFPHashObjectList)
  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. Function CheckOperand: boolean; virtual;
  89. Procedure InitRef;
  90. end;
  91. TCOperand = class of TOperand;
  92. TInstruction = class
  93. opcode : tasmop;
  94. condition : tasmcond;
  95. ops : byte;
  96. labeled : boolean;
  97. operands : array[1..max_operands] of toperand;
  98. constructor create(optype : tcoperand);virtual;
  99. destructor destroy;override;
  100. { converts the instruction to an instruction how it's used by the assembler writer
  101. and concats it to the passed list. The newly created item is returned if the
  102. instruction was valid, otherwise nil is returned }
  103. function ConcatInstruction(p:TAsmList) : tai;virtual;
  104. Procedure Swapoperands;
  105. end;
  106. {---------------------------------------------------------------------}
  107. { Expression parser types }
  108. {---------------------------------------------------------------------}
  109. TExprOperator = record
  110. ch: char; { operator }
  111. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  112. end;
  113. {**********************************************************************}
  114. { The following operators are supported: }
  115. { '+' : addition }
  116. { '-' : subtraction }
  117. { '*' : multiplication }
  118. { '/' : modulo division }
  119. { '^' : exclusive or }
  120. { '<' : shift left }
  121. { '>' : shift right }
  122. { '&' : bitwise and }
  123. { '|' : bitwise or }
  124. { '~' : bitwise complement }
  125. { '%' : modulo division }
  126. { nnn: longint numbers }
  127. { ( and ) parenthesis }
  128. {**********************************************************************}
  129. TExprParse = class
  130. public
  131. Constructor create;
  132. Destructor Destroy;override;
  133. Function Evaluate(Expr: String): aint;
  134. Function Priority(_Operator: Char): aint;
  135. private
  136. RPNStack : Array[1..RPNMax] of aint; { Stack For RPN calculator }
  137. RPNTop : aint;
  138. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  139. OpTop : aint;
  140. Procedure RPNPush(Num: aint);
  141. Function RPNPop: aint;
  142. Procedure RPNCalc(const token: String; prefix: boolean);
  143. Procedure OpPush(_Operator: char; prefix: boolean);
  144. { In reality returns TExprOperaotr }
  145. Procedure OpPop(var _Operator:TExprOperator);
  146. end;
  147. { Evaluate an expression string to a aint }
  148. Function CalculateExpression(const expression: string): aint;
  149. {---------------------------------------------------------------------}
  150. { String routines }
  151. {---------------------------------------------------------------------}
  152. Function ParseVal(const S:String;base:byte):aint;
  153. Function PadZero(Var s: String; n: byte): Boolean;
  154. Function EscapeToPascal(const s:string): string;
  155. {---------------------------------------------------------------------
  156. Symbol helper routines
  157. ---------------------------------------------------------------------}
  158. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:TSymtable);
  159. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint; var mangledname: string; needvmtofs: boolean):boolean;
  160. Function SearchType(const hs:string;var size:aint): Boolean;
  161. Function SearchRecordType(const s:string): boolean;
  162. Function SearchIConstant(const s:string; var l:aint): boolean;
  163. {---------------------------------------------------------------------
  164. Instruction generation routines
  165. ---------------------------------------------------------------------}
  166. Procedure ConcatPasString(p : TAsmList;s:string);
  167. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  168. Procedure ConcatConstant(p : TAsmList;value: aint; constsize:byte);
  169. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:aint);
  170. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  171. Procedure ConcatString(p : TAsmList;s:string);
  172. procedure ConcatAlign(p:TAsmList;l:aint);
  173. Procedure ConcatPublic(p:TAsmList;const s : string);
  174. Procedure ConcatLocal(p:TAsmList;const s : string);
  175. Implementation
  176. uses
  177. SysUtils,
  178. defutil,systems,verbose,globals,
  179. symtable,paramgr,
  180. aasmcpu,
  181. procinfo;
  182. {*************************************************************************
  183. TExprParse
  184. *************************************************************************}
  185. Constructor TExprParse.create;
  186. Begin
  187. end;
  188. Procedure TExprParse.RPNPush(Num : aint);
  189. { Add an operand to the top of the RPN stack }
  190. begin
  191. if RPNTop < RPNMax then
  192. begin
  193. Inc(RPNTop);
  194. RPNStack[RPNTop]:=Num;
  195. end
  196. else
  197. Message(asmr_e_expr_illegal);
  198. end;
  199. Function TExprParse.RPNPop : aint;
  200. { Get the operand at the top of the RPN stack }
  201. begin
  202. if RPNTop > 0 then
  203. begin
  204. RPNPop:=RPNStack[RPNTop];
  205. Dec(RPNTop);
  206. end
  207. else
  208. Message(asmr_e_expr_illegal);
  209. end;
  210. Procedure TExprParse.RPNCalc(const Token : String; prefix:boolean); { RPN Calculator }
  211. Var
  212. Temp : aint;
  213. n1,n2 : aint;
  214. LocalError : Integer;
  215. begin
  216. { Handle operators }
  217. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  218. Case Token[1] of
  219. '+' :
  220. Begin
  221. if not prefix then
  222. RPNPush(RPNPop + RPNPop);
  223. end;
  224. '-' :
  225. Begin
  226. if prefix then
  227. RPNPush(-(RPNPop))
  228. else
  229. begin
  230. n1:=RPNPop;
  231. n2:=RPNPop;
  232. RPNPush(n2 - n1);
  233. end;
  234. end;
  235. '*' : RPNPush(RPNPop * RPNPop);
  236. '&' :
  237. begin
  238. n1:=RPNPop;
  239. n2:=RPNPop;
  240. RPNPush(n2 and n1);
  241. end;
  242. '|' :
  243. begin
  244. n1:=RPNPop;
  245. n2:=RPNPop;
  246. RPNPush(n2 or n1);
  247. end;
  248. '~' : RPNPush(NOT RPNPop);
  249. '<' :
  250. begin
  251. n1:=RPNPop;
  252. n2:=RPNPop;
  253. RPNPush(n2 SHL n1);
  254. end;
  255. '>' :
  256. begin
  257. n1:=RPNPop;
  258. n2:=RPNPop;
  259. RPNPush(n2 SHR n1);
  260. end;
  261. '%' :
  262. begin
  263. Temp:=RPNPop;
  264. if Temp <> 0 then
  265. RPNPush(RPNPop mod Temp)
  266. else
  267. begin
  268. Message(asmr_e_expr_zero_divide);
  269. { push 1 for error recovery }
  270. RPNPush(1);
  271. end;
  272. end;
  273. '^' : RPNPush(RPNPop XOR RPNPop);
  274. '/' :
  275. begin
  276. Temp:=RPNPop;
  277. if Temp <> 0 then
  278. RPNPush(RPNPop div Temp)
  279. else
  280. begin
  281. Message(asmr_e_expr_zero_divide);
  282. { push 1 for error recovery }
  283. RPNPush(1);
  284. end;
  285. end;
  286. end
  287. else
  288. begin
  289. { Convert String to number and add to stack }
  290. Val(Token, Temp, LocalError);
  291. if LocalError = 0 then
  292. RPNPush(Temp)
  293. else
  294. begin
  295. Message(asmr_e_expr_illegal);
  296. { push 1 for error recovery }
  297. RPNPush(1);
  298. end;
  299. end;
  300. end;
  301. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  302. { Add an operator onto top of the stack }
  303. begin
  304. if OpTop < OpMax then
  305. begin
  306. Inc(OpTop);
  307. OpStack[OpTop].ch:=_Operator;
  308. OpStack[OpTop].is_prefix:=prefix;
  309. end
  310. else
  311. Message(asmr_e_expr_illegal);
  312. end;
  313. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  314. { Get operator at the top of the stack }
  315. begin
  316. if OpTop > 0 then
  317. begin
  318. _Operator:=OpStack[OpTop];
  319. Dec(OpTop);
  320. end
  321. else
  322. Message(asmr_e_expr_illegal);
  323. end;
  324. Function TExprParse.Priority(_Operator : Char) : aint;
  325. { Return priority of operator }
  326. { The greater the priority, the higher the precedence }
  327. begin
  328. Case _Operator OF
  329. '(' :
  330. Priority:=0;
  331. '+', '-' :
  332. Priority:=1;
  333. '*', '/','%','<','>' :
  334. Priority:=2;
  335. '|','&','^','~' :
  336. Priority:=0;
  337. else
  338. Message(asmr_e_expr_illegal);
  339. end;
  340. end;
  341. Function TExprParse.Evaluate(Expr : String):aint;
  342. Var
  343. I : longint;
  344. Token : String;
  345. opr : TExprOperator;
  346. begin
  347. Evaluate:=0;
  348. { Reset stacks }
  349. OpTop :=0;
  350. RPNTop:=0;
  351. Token :='';
  352. { nothing to do ? }
  353. if Expr='' then
  354. exit;
  355. For I:=1 to Length(Expr) DO
  356. begin
  357. if Expr[I] in ['0'..'9'] then
  358. begin { Build multi-digit numbers }
  359. Token:=Token + Expr[I];
  360. if I = Length(Expr) then { Send last one to calculator }
  361. RPNCalc(Token,false);
  362. end
  363. else
  364. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  365. begin
  366. if Token <> '' then
  367. begin { Send last built number to calc. }
  368. RPNCalc(Token,false);
  369. Token:='';
  370. end;
  371. Case Expr[I] OF
  372. '(' : OpPush('(',false);
  373. ')' : begin
  374. While (OpTop>0) and (OpStack[OpTop].ch <> '(') DO
  375. Begin
  376. OpPop(opr);
  377. RPNCalc(opr.ch,opr.is_prefix);
  378. end;
  379. OpPop(opr); { Pop off and ignore the '(' }
  380. end;
  381. '+','-','~' : Begin
  382. { workaround for -2147483648 }
  383. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  384. begin
  385. token:='-';
  386. expr[i]:='+';
  387. end;
  388. { if start of expression then surely a prefix }
  389. { or if previous char was also an operator }
  390. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  391. OpPush(Expr[I],true)
  392. else
  393. Begin
  394. { Evaluate all higher priority operators }
  395. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  396. Begin
  397. OpPop(opr);
  398. RPNCalc(opr.ch,opr.is_prefix);
  399. end;
  400. OpPush(Expr[I],false);
  401. End;
  402. end;
  403. '*', '/',
  404. '^','|','&',
  405. '%','<','>' : begin
  406. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  407. Begin
  408. OpPop(opr);
  409. RPNCalc(opr.ch,opr.is_prefix);
  410. end;
  411. OpPush(Expr[I],false);
  412. end;
  413. end; { Case }
  414. end
  415. else
  416. Message(asmr_e_expr_illegal); { Handle bad input error }
  417. end;
  418. { Pop off the remaining operators }
  419. While OpTop > 0 do
  420. Begin
  421. OpPop(opr);
  422. RPNCalc(opr.ch,opr.is_prefix);
  423. end;
  424. { The result is stored on the top of the stack }
  425. Evaluate:=RPNPop;
  426. end;
  427. Destructor TExprParse.Destroy;
  428. Begin
  429. end;
  430. Function CalculateExpression(const expression: string): aint;
  431. var
  432. expr: TExprParse;
  433. Begin
  434. expr:=TExprParse.create;
  435. CalculateExpression:=expr.Evaluate(expression);
  436. expr.Free;
  437. end;
  438. {*************************************************************************}
  439. { String conversions/utils }
  440. {*************************************************************************}
  441. Function EscapeToPascal(const s:string): string;
  442. { converts a C styled string - which contains escape }
  443. { characters to a pascal style string. }
  444. var
  445. i,len : aint;
  446. hs : string;
  447. temp : string;
  448. c : char;
  449. Begin
  450. hs:='';
  451. len:=0;
  452. i:=0;
  453. while (i<length(s)) and (len<255) do
  454. begin
  455. Inc(i);
  456. if (s[i]='\') and (i<length(s)) then
  457. Begin
  458. inc(i);
  459. case s[i] of
  460. '\' :
  461. c:='\';
  462. 'b':
  463. c:=#8;
  464. 'f':
  465. c:=#12;
  466. 'n':
  467. c:=#10;
  468. 'r':
  469. c:=#13;
  470. 't':
  471. c:=#9;
  472. '"':
  473. c:='"';
  474. '0'..'7':
  475. Begin
  476. temp:=s[i];
  477. temp:=temp+s[i+1];
  478. temp:=temp+s[i+2];
  479. inc(i,2);
  480. c:=chr(ParseVal(temp,8));
  481. end;
  482. 'x':
  483. Begin
  484. temp:=s[i+1];
  485. temp:=temp+s[i+2];
  486. inc(i,2);
  487. c:=chr(ParseVal(temp,16));
  488. end;
  489. else
  490. Begin
  491. Message1(asmr_e_escape_seq_ignored,s[i]);
  492. c:=s[i];
  493. end;
  494. end;
  495. end
  496. else
  497. c:=s[i];
  498. inc(len);
  499. hs[len]:=c;
  500. end;
  501. hs[0]:=chr(len);
  502. EscapeToPascal:=hs;
  503. end;
  504. Function ParseVal(const S:String;base:byte):aint;
  505. { Converts a decimal string to aint }
  506. var
  507. code : integer;
  508. errmsg : word;
  509. prefix : string[2];
  510. Begin
  511. case base of
  512. 2 :
  513. begin
  514. errmsg:=asmr_e_error_converting_binary;
  515. prefix:='%';
  516. end;
  517. 8 :
  518. begin
  519. errmsg:=asmr_e_error_converting_octal;
  520. prefix:='&';
  521. end;
  522. 10 :
  523. begin
  524. errmsg:=asmr_e_error_converting_decimal;
  525. prefix:='';
  526. end;
  527. 16 :
  528. begin
  529. errmsg:=asmr_e_error_converting_hexadecimal;
  530. prefix:='$';
  531. end;
  532. else
  533. internalerror(200501202);
  534. end;
  535. val(prefix+s,result,code);
  536. if code<>0 then
  537. begin
  538. val(prefix+s,aword(result),code);
  539. if code<>0 then
  540. begin
  541. Message1(errmsg,s);
  542. result:=0;
  543. end;
  544. end;
  545. end;
  546. Function PadZero(Var s: String; n: byte): Boolean;
  547. Begin
  548. PadZero:=TRUE;
  549. { Do some error checking first }
  550. if Length(s) = n then
  551. exit
  552. else
  553. if Length(s) > n then
  554. Begin
  555. PadZero:=FALSE;
  556. delete(s,n+1,length(s));
  557. exit;
  558. end
  559. else
  560. PadZero:=TRUE;
  561. { Fill it up with the specified character }
  562. fillchar(s[length(s)+1],n-1,#0);
  563. s[0]:=chr(n);
  564. end;
  565. {****************************************************************************
  566. TOperand
  567. ****************************************************************************}
  568. constructor TOperand.Create;
  569. begin
  570. size:=OS_NO;
  571. hastype:=false;
  572. hasvar:=false;
  573. FillChar(Opr,sizeof(Opr),0);
  574. end;
  575. destructor TOperand.destroy;
  576. begin
  577. end;
  578. Procedure TOperand.SetSize(_size:longint;force:boolean);
  579. begin
  580. if force or
  581. ((size = OS_NO) and (_size<=16)) then
  582. Begin
  583. case _size of
  584. 1 : size:=OS_8;
  585. 2 : size:=OS_16{ could be S_IS};
  586. 4 : size:=OS_32{ could be S_IL or S_FS};
  587. 8 : size:=OS_64{ could be S_D or S_FL};
  588. 10 : size:=OS_F80;
  589. 16 : size:=OS_128;
  590. end;
  591. end;
  592. end;
  593. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  594. begin
  595. end;
  596. function TOperand.SetupResult:boolean;
  597. begin
  598. SetupResult:=false;
  599. { replace by correct offset. }
  600. with current_procinfo.procdef do
  601. if (not is_void(returndef)) then
  602. begin
  603. if (m_tp7 in current_settings.modeswitches) and
  604. (not paramanager.ret_in_param(returndef,proccalloption)) then
  605. begin
  606. message(asmr_e_cannot_use_RESULT_here);
  607. exit;
  608. end;
  609. SetupResult:=setupvar('result',false)
  610. end
  611. else
  612. message(asmr_e_void_function);
  613. end;
  614. Function TOperand.SetupSelf:boolean;
  615. Begin
  616. SetupSelf:=false;
  617. if assigned(current_procinfo.procdef._class) then
  618. SetupSelf:=setupvar('self',false)
  619. else
  620. Message(asmr_e_cannot_use_SELF_outside_a_method);
  621. end;
  622. Function TOperand.SetupOldEBP:boolean;
  623. Begin
  624. SetupOldEBP:=false;
  625. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  626. SetupOldEBP:=setupvar('parentframe',false)
  627. else
  628. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  629. end;
  630. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  631. function symtable_has_localvarsyms(st:TSymtable):boolean;
  632. var
  633. sym : tsym;
  634. i : longint;
  635. begin
  636. result:=false;
  637. for i:=0 to st.SymList.Count-1 do
  638. begin
  639. sym:=tsym(st.SymList[i]);
  640. if sym.typ=localvarsym then
  641. begin
  642. result:=true;
  643. exit;
  644. end;
  645. end;
  646. end;
  647. procedure setconst(l:aint);
  648. begin
  649. { We return the address of the field, just like Delphi/TP }
  650. case opr.typ of
  651. OPR_NONE :
  652. begin
  653. opr.typ:=OPR_CONSTANT;
  654. opr.val:=l;
  655. end;
  656. OPR_CONSTANT :
  657. inc(opr.val,l);
  658. OPR_REFERENCE :
  659. inc(opr.ref.offset,l);
  660. OPR_LOCAL :
  661. inc(opr.localsymofs,l);
  662. else
  663. Message(asmr_e_invalid_operand_type);
  664. end;
  665. end;
  666. { search and sets up the correct fields in the Instr record }
  667. { for the NON-constant identifier passed to the routine. }
  668. { if not found returns FALSE. }
  669. var
  670. sym : tsym;
  671. srsymtable : TSymtable;
  672. harrdef : tarraydef;
  673. indexreg : tregister;
  674. l : aint;
  675. plist : ppropaccesslistitem;
  676. Begin
  677. SetupVar:=false;
  678. asmsearchsym(s,sym,srsymtable);
  679. if sym = nil then
  680. exit;
  681. if sym.typ=absolutevarsym then
  682. begin
  683. if (tabsolutevarsym(sym).abstyp=tovar) then
  684. begin
  685. { Only support simple loads }
  686. plist:=tabsolutevarsym(sym).ref.firstsym;
  687. if assigned(plist) and
  688. (plist^.sltype=sl_load) then
  689. sym:=plist^.sym
  690. else
  691. begin
  692. Message(asmr_e_unsupported_symbol_type);
  693. exit;
  694. end;
  695. end
  696. else
  697. begin
  698. Message(asmr_e_unsupported_symbol_type);
  699. exit;
  700. end;
  701. end;
  702. case sym.typ of
  703. fieldvarsym :
  704. begin
  705. setconst(tfieldvarsym(sym).fieldoffset);
  706. hasvar:=true;
  707. SetupVar:=true;
  708. end;
  709. staticvarsym,
  710. localvarsym,
  711. paravarsym :
  712. begin
  713. { we always assume in asm statements that }
  714. { that the variable is valid. }
  715. tabstractvarsym(sym).varstate:=vs_readwritten;
  716. inc(tabstractvarsym(sym).refs);
  717. { variable can't be placed in a register }
  718. tabstractvarsym(sym).varregable:=vr_none;
  719. { and anything may happen with its address }
  720. tabstractvarsym(sym).addr_taken:=true;
  721. case sym.typ of
  722. staticvarsym :
  723. begin
  724. initref;
  725. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(sym).mangledname);
  726. end;
  727. paravarsym,
  728. localvarsym :
  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.typ of
  761. orddef,
  762. enumdef,
  763. pointerdef,
  764. floatdef :
  765. SetSize(tabstractvarsym(sym).getsize,false);
  766. arraydef :
  767. begin
  768. { for arrays try to get the element size, take care of
  769. multiple indexes }
  770. harrdef:=tarraydef(tabstractvarsym(sym).vardef);
  771. while assigned(harrdef.elementdef) and
  772. (harrdef.elementdef.typ=arraydef) do
  773. harrdef:=tarraydef(harrdef.elementdef);
  774. if not is_packed_array(harrdef) then
  775. SetSize(harrdef.elesize,false)
  776. else
  777. begin
  778. if (harrdef.elepackedbitsize mod 8) = 0 then
  779. SetSize(harrdef.elepackedbitsize div 8,false);
  780. end;
  781. end;
  782. end;
  783. hasvar:=true;
  784. SetupVar:=true;
  785. Exit;
  786. end;
  787. constsym :
  788. begin
  789. if tconstsym(sym).consttyp=constord then
  790. begin
  791. setconst(tconstsym(sym).value.valueord.svalue);
  792. SetupVar:=true;
  793. Exit;
  794. end;
  795. end;
  796. typesym :
  797. begin
  798. if ttypesym(sym).typedef.typ in [recorddef,objectdef] then
  799. begin
  800. setconst(0);
  801. SetupVar:=TRUE;
  802. Exit;
  803. end;
  804. end;
  805. procsym :
  806. begin
  807. if opr.typ<>OPR_NONE then
  808. Message(asmr_e_invalid_operand_type);
  809. if Tprocsym(sym).ProcdefList.Count>1 then
  810. Message(asmr_w_calling_overload_func);
  811. l:=opr.ref.offset;
  812. opr.typ:=OPR_SYMBOL;
  813. opr.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname);
  814. opr.symofs:=l;
  815. hasvar:=true;
  816. SetupVar:=TRUE;
  817. Exit;
  818. end;
  819. else
  820. begin
  821. Message(asmr_e_unsupported_symbol_type);
  822. exit;
  823. end;
  824. end;
  825. end;
  826. procedure TOperand.InitRef;
  827. {*********************************************************************}
  828. { Description: This routine first check if the opcode is of }
  829. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  830. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  831. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  832. { to point to the default segment. }
  833. {*********************************************************************}
  834. var
  835. l : aint;
  836. hsymofs : aint;
  837. hsymbol : tasmsymbol;
  838. reg : tregister;
  839. Begin
  840. case opr.typ of
  841. OPR_REFERENCE :
  842. exit;
  843. OPR_CONSTANT :
  844. begin
  845. l:=opr.val;
  846. opr.typ:=OPR_REFERENCE;
  847. Fillchar(opr.ref,sizeof(treference),0);
  848. opr.Ref.Offset:=l;
  849. end;
  850. OPR_NONE :
  851. begin
  852. opr.typ:=OPR_REFERENCE;
  853. Fillchar(opr.ref,sizeof(treference),0);
  854. end;
  855. OPR_REGISTER :
  856. begin
  857. reg:=opr.reg;
  858. opr.typ:=OPR_REFERENCE;
  859. Fillchar(opr.ref,sizeof(treference),0);
  860. opr.Ref.base:=reg;
  861. end;
  862. OPR_SYMBOL :
  863. begin
  864. hsymbol:=opr.symbol;
  865. hsymofs:=opr.symofs;
  866. opr.typ:=OPR_REFERENCE;
  867. Fillchar(opr.ref,sizeof(treference),0);
  868. opr.ref.symbol:=hsymbol;
  869. opr.ref.offset:=hsymofs;
  870. end;
  871. else
  872. begin
  873. Message(asmr_e_invalid_operand_type);
  874. { Recover }
  875. opr.typ:=OPR_REFERENCE;
  876. Fillchar(opr.ref,sizeof(treference),0);
  877. end;
  878. end;
  879. end;
  880. Function TOperand.CheckOperand: boolean;
  881. {*********************************************************************}
  882. { Description: This routine checks if the operand is of }
  883. { valid, and returns false if it isn't. Does nothing by default. }
  884. {*********************************************************************}
  885. begin
  886. result:=true;
  887. end;
  888. {****************************************************************************
  889. TInstruction
  890. ****************************************************************************}
  891. constructor TInstruction.create(optype : tcoperand);
  892. var
  893. i : longint;
  894. Begin
  895. { these field are set to 0 anyways by the constructor helper (FK)
  896. Opcode:=A_NONE;
  897. Condition:=C_NONE;
  898. Ops:=0;
  899. }
  900. for i:=1 to max_operands do
  901. Operands[i]:=optype.create;
  902. Labeled:=false;
  903. end;
  904. destructor TInstruction.destroy;
  905. var
  906. i : longint;
  907. Begin
  908. for i:=1 to max_operands do
  909. Operands[i].free;
  910. end;
  911. Procedure TInstruction.Swapoperands;
  912. Var
  913. p : toperand;
  914. Begin
  915. case Ops of
  916. 2 :
  917. begin
  918. p:=Operands[1];
  919. Operands[1]:=Operands[2];
  920. Operands[2]:=p;
  921. end;
  922. 3 :
  923. begin
  924. p:=Operands[1];
  925. Operands[1]:=Operands[3];
  926. Operands[3]:=p;
  927. end;
  928. end;
  929. end;
  930. function TInstruction.ConcatInstruction(p:TAsmList) : tai;
  931. var
  932. ai : taicpu;
  933. i : longint;
  934. begin
  935. for i:=1 to Ops do
  936. operands[i].CheckOperand;
  937. ai:=taicpu.op_none(opcode);
  938. ai.Ops:=Ops;
  939. ai.Allocate_oper(Ops);
  940. for i:=1 to Ops do
  941. with operands[i].opr do
  942. begin
  943. case typ of
  944. OPR_CONSTANT :
  945. ai.loadconst(i-1,val);
  946. OPR_REGISTER:
  947. ai.loadreg(i-1,reg);
  948. OPR_SYMBOL:
  949. ai.loadsymbol(i-1,symbol,symofs);
  950. OPR_LOCAL :
  951. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  952. localscale,localgetoffset,localforceref);
  953. OPR_REFERENCE:
  954. ai.loadref(i-1,ref);
  955. {$ifdef ARM}
  956. OPR_REGSET:
  957. ai.loadregset(i-1,regset);
  958. OPR_SHIFTEROP:
  959. ai.loadshifterop(i-1,shifterop);
  960. {$endif ARM}
  961. else
  962. internalerror(200501051);
  963. end;
  964. end;
  965. ai.SetCondition(condition);
  966. { Concat the opcode or give an error }
  967. if assigned(ai) then
  968. p.concat(ai)
  969. else
  970. Message(asmr_e_invalid_opcode_and_operand);
  971. result:=ai;
  972. end;
  973. {***************************************************************************
  974. TLocalLabel
  975. ***************************************************************************}
  976. constructor TLocalLabel.create(AList:TFPHashObjectList;const n:string);
  977. begin
  978. inherited Create(AList,n);
  979. lab:=nil;
  980. emitted:=false;
  981. end;
  982. function TLocalLabel.Gettasmlabel:tasmlabel;
  983. begin
  984. if not assigned(lab) then
  985. begin
  986. current_asmdata.getjumplabel(lab);
  987. { this label is forced to be used so it's always written }
  988. lab.increfs;
  989. end;
  990. Gettasmlabel:=lab;
  991. end;
  992. {***************************************************************************
  993. TLocalLabelList
  994. ***************************************************************************}
  995. procedure TLocalLabelList.CheckEmitted;
  996. var
  997. i : longint;
  998. lab : TLocalLabel;
  999. begin
  1000. for i:=0 to LocalLabelList.Count-1 do
  1001. begin
  1002. lab:=TLocalLabel(LocalLabelList[i]);
  1003. if not lab.emitted then
  1004. Message1(asmr_e_unknown_label_identifier,lab.name);
  1005. end;
  1006. end;
  1007. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1008. var
  1009. lab : TLocalLabel;
  1010. Begin
  1011. CreateLocalLabel:=true;
  1012. { Check if it already is defined }
  1013. lab:=TLocalLabel(LocalLabellist.Find(s));
  1014. if not assigned(lab) then
  1015. lab:=TLocalLabel.Create(LocalLabellist,s);
  1016. { set emitted flag and check for dup syms }
  1017. if emit then
  1018. begin
  1019. if lab.Emitted then
  1020. begin
  1021. Message1(asmr_e_dup_local_sym,lab.Name);
  1022. CreateLocalLabel:=false;
  1023. end;
  1024. lab.Emitted:=true;
  1025. end;
  1026. hl:=lab.Gettasmlabel;
  1027. end;
  1028. {****************************************************************************
  1029. Symbol table helper routines
  1030. ****************************************************************************}
  1031. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:TSymtable);
  1032. var
  1033. i : integer;
  1034. begin
  1035. i:=pos('.',s);
  1036. { allow unit.identifier }
  1037. if i>0 then
  1038. begin
  1039. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1040. if assigned(srsym) then
  1041. begin
  1042. if (srsym.typ=unitsym) and
  1043. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  1044. srsym.owner.iscurrentunit then
  1045. searchsym_in_module(tunitsym(srsym).module,Copy(s,i+1,255),srsym,srsymtable)
  1046. else
  1047. begin
  1048. srsym:=nil;
  1049. srsymtable:=nil;
  1050. end;
  1051. end;
  1052. end
  1053. else
  1054. searchsym(s,srsym,srsymtable);
  1055. end;
  1056. Function SearchType(const hs:string;var size:aint): Boolean;
  1057. var
  1058. srsym : tsym;
  1059. srsymtable : TSymtable;
  1060. begin
  1061. result:=false;
  1062. size:=0;
  1063. asmsearchsym(hs,srsym,srsymtable);
  1064. if assigned(srsym) and
  1065. (srsym.typ=typesym) then
  1066. begin
  1067. size:=ttypesym(srsym).typedef.size;
  1068. result:=true;
  1069. end;
  1070. end;
  1071. Function SearchRecordType(const s:string): boolean;
  1072. var
  1073. srsym : tsym;
  1074. srsymtable : TSymtable;
  1075. Begin
  1076. SearchRecordType:=false;
  1077. { Check the constants in symtable }
  1078. asmsearchsym(s,srsym,srsymtable);
  1079. if srsym <> nil then
  1080. Begin
  1081. case srsym.typ of
  1082. typesym :
  1083. begin
  1084. if ttypesym(srsym).typedef.typ in [recorddef,objectdef] then
  1085. begin
  1086. SearchRecordType:=true;
  1087. exit;
  1088. end;
  1089. end;
  1090. fieldvarsym :
  1091. begin
  1092. if (tfieldvarsym(srsym).vardef.typ in [recorddef,objectdef]) then
  1093. begin
  1094. SearchRecordType:=true;
  1095. exit;
  1096. end;
  1097. end;
  1098. end;
  1099. end;
  1100. end;
  1101. Function SearchIConstant(const s:string; var l:aint): boolean;
  1102. {**********************************************************************}
  1103. { Description: Searches for a CONSTANT of name s in either the local }
  1104. { symbol list, then in the global symbol list, and returns the value }
  1105. { of that constant in l. Returns TRUE if successfull, if not found, }
  1106. { or if the constant is not of correct type, then returns FALSE }
  1107. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1108. { respectively. }
  1109. {**********************************************************************}
  1110. var
  1111. srsym : tsym;
  1112. srsymtable : TSymtable;
  1113. Begin
  1114. SearchIConstant:=false;
  1115. { check for TRUE or FALSE reserved words first }
  1116. if s = 'TRUE' then
  1117. Begin
  1118. SearchIConstant:=TRUE;
  1119. l:=1;
  1120. exit;
  1121. end;
  1122. if s = 'FALSE' then
  1123. Begin
  1124. SearchIConstant:=TRUE;
  1125. l:=0;
  1126. exit;
  1127. end;
  1128. { Check the constants in symtable }
  1129. asmsearchsym(s,srsym,srsymtable);
  1130. if srsym <> nil then
  1131. Begin
  1132. case srsym.typ of
  1133. constsym :
  1134. begin
  1135. if tconstsym(srsym).consttyp=constord then
  1136. Begin
  1137. l:=tconstsym(srsym).value.valueord.svalue;
  1138. SearchIConstant:=TRUE;
  1139. exit;
  1140. end;
  1141. end;
  1142. enumsym:
  1143. Begin
  1144. l:=tenumsym(srsym).value;
  1145. SearchIConstant:=TRUE;
  1146. exit;
  1147. end;
  1148. end;
  1149. end;
  1150. end;
  1151. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint; var mangledname: string; needvmtofs: boolean):boolean;
  1152. { search and returns the offset and size of records/objects of the base }
  1153. { with field name setup in field. }
  1154. { returns FALSE if not found. }
  1155. { used when base is a variable or a typed constant name. }
  1156. var
  1157. st : TSymtable;
  1158. harrdef : tarraydef;
  1159. sym : tsym;
  1160. srsymtable : TSymtable;
  1161. i : longint;
  1162. base : string;
  1163. procdef: tprocdef;
  1164. Begin
  1165. GetRecordOffsetSize:=FALSE;
  1166. Offset:=0;
  1167. Size:=0;
  1168. mangledname:='';
  1169. i:=pos('.',s);
  1170. if i=0 then
  1171. i:=255;
  1172. base:=Copy(s,1,i-1);
  1173. delete(s,1,i);
  1174. if base='SELF' then
  1175. st:=current_procinfo.procdef._class.symtable
  1176. else
  1177. begin
  1178. asmsearchsym(base,sym,srsymtable);
  1179. st:=nil;
  1180. { we can start with a var,type,typedconst }
  1181. if assigned(sym) then
  1182. case sym.typ of
  1183. staticvarsym,
  1184. localvarsym,
  1185. paravarsym :
  1186. st:=Tabstractvarsym(sym).vardef.GetSymtable(gs_record);
  1187. typesym :
  1188. st:=Ttypesym(sym).typedef.GetSymtable(gs_record);
  1189. end
  1190. else
  1191. s:='';
  1192. end;
  1193. { now walk all recordsymtables }
  1194. while assigned(st) and (s<>'') do
  1195. begin
  1196. { load next field in base }
  1197. i:=pos('.',s);
  1198. if i=0 then
  1199. i:=255;
  1200. base:=Copy(s,1,i-1);
  1201. delete(s,1,i);
  1202. if st.symtabletype=ObjectSymtable then
  1203. sym:=search_class_member(tobjectdef(st.defowner),base)
  1204. else
  1205. sym:=tsym(st.Find(base));
  1206. if not assigned(sym) then
  1207. begin
  1208. GetRecordOffsetSize:=false;
  1209. exit;
  1210. end;
  1211. st:=nil;
  1212. case sym.typ of
  1213. fieldvarsym :
  1214. with Tfieldvarsym(sym) do
  1215. begin
  1216. inc(Offset,fieldoffset);
  1217. size:=getsize;
  1218. case vardef.typ of
  1219. arraydef :
  1220. begin
  1221. { for arrays try to get the element size, take care of
  1222. multiple indexes }
  1223. harrdef:=tarraydef(vardef);
  1224. while assigned(harrdef.elementdef) and
  1225. (harrdef.elementdef.typ=arraydef) do
  1226. harrdef:=tarraydef(harrdef.elementdef);
  1227. if not is_packed_array(harrdef) then
  1228. size:=harrdef.elesize
  1229. else
  1230. begin
  1231. if (harrdef.elepackedbitsize mod 8) <> 0 then
  1232. Message(asmr_e_packed_element);
  1233. size := (harrdef.elepackedbitsize + 7) div 8;
  1234. end;
  1235. end;
  1236. recorddef :
  1237. st:=trecorddef(vardef).symtable;
  1238. objectdef :
  1239. st:=tobjectdef(vardef).symtable;
  1240. end;
  1241. end;
  1242. procsym:
  1243. begin
  1244. st:=nil;
  1245. if Tprocsym(sym).ProcdefList.Count>1 then
  1246. Message(asmr_w_calling_overload_func);
  1247. procdef:=tprocdef(tprocsym(sym).ProcdefList[0]);
  1248. if (not needvmtofs) then
  1249. begin
  1250. mangledname:=procdef.mangledname;
  1251. end
  1252. else
  1253. begin
  1254. { can only get the vmtoffset of virtual methods }
  1255. if not(po_virtualmethod in procdef.procoptions) then
  1256. Message1(asmr_e_no_vmtoffset_possible,FullTypeName(procdef,nil))
  1257. else
  1258. begin
  1259. { size = sizeof(target_system_pointer) }
  1260. size:=sizeof(aint);
  1261. offset:=procdef._class.vmtmethodoffset(procdef.extnumber)
  1262. end;
  1263. end;
  1264. { if something comes after the procsym, it's invalid assembler syntax }
  1265. GetRecordOffsetSize:=(s='');
  1266. exit;
  1267. end;
  1268. end;
  1269. end;
  1270. { Support Field.Type as typecasting }
  1271. if (st=nil) and (s<>'') then
  1272. begin
  1273. asmsearchsym(s,sym,srsymtable);
  1274. if assigned(sym) and (sym.typ=typesym) then
  1275. begin
  1276. size:=ttypesym(sym).typedef.size;
  1277. s:=''
  1278. end;
  1279. end;
  1280. GetRecordOffsetSize:=(s='');
  1281. end;
  1282. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1283. var
  1284. sym : tsym;
  1285. srsymtable : TSymtable;
  1286. hs : string;
  1287. Begin
  1288. hl:=nil;
  1289. SearchLabel:=false;
  1290. { Check for pascal labels, which are case insensetive }
  1291. hs:=upper(s);
  1292. asmsearchsym(hs,sym,srsymtable);
  1293. if sym=nil then
  1294. exit;
  1295. case sym.typ of
  1296. labelsym :
  1297. begin
  1298. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1299. begin
  1300. Tlabelsym(sym).nonlocal:=true;
  1301. if emit then
  1302. exclude(current_procinfo.procdef.procoptions,po_inline);
  1303. end;
  1304. if not(assigned(tlabelsym(sym).asmblocklabel)) then
  1305. if Tlabelsym(sym).nonlocal then
  1306. current_asmdata.getglobaljumplabel(tlabelsym(sym).asmblocklabel)
  1307. else
  1308. current_asmdata.getjumplabel(tlabelsym(sym).asmblocklabel);
  1309. hl:=tlabelsym(sym).asmblocklabel;
  1310. if emit then
  1311. tlabelsym(sym).defined:=true
  1312. else
  1313. tlabelsym(sym).used:=true;
  1314. SearchLabel:=true;
  1315. end;
  1316. end;
  1317. end;
  1318. {*************************************************************************}
  1319. { Instruction Generation Utilities }
  1320. {*************************************************************************}
  1321. Procedure ConcatString(p : TAsmList;s:string);
  1322. {*********************************************************************}
  1323. { PROCEDURE ConcatString(s:string); }
  1324. { Description: This routine adds the character chain pointed to in }
  1325. { s to the instruction linked list. }
  1326. {*********************************************************************}
  1327. Var
  1328. pc: PChar;
  1329. Begin
  1330. getmem(pc,length(s)+1);
  1331. p.concat(Tai_string.Create_pchar(strpcopy(pc,s),length(s)));
  1332. end;
  1333. Procedure ConcatPasString(p : TAsmList;s:string);
  1334. {*********************************************************************}
  1335. { PROCEDURE ConcatPasString(s:string); }
  1336. { Description: This routine adds the character chain pointed to in }
  1337. { s to the instruction linked list, contrary to ConcatString it }
  1338. { uses a pascal style string, so it conserves null characters. }
  1339. {*********************************************************************}
  1340. Begin
  1341. p.concat(Tai_string.Create(s));
  1342. end;
  1343. Procedure ConcatConstant(p: TAsmList; value: aint; constsize:byte);
  1344. {*********************************************************************}
  1345. { PROCEDURE ConcatConstant(value: aint; maxvalue: aint); }
  1346. { Description: This routine adds the value constant to the current }
  1347. { instruction linked list. }
  1348. { maxvalue -> indicates the size of the data to initialize: }
  1349. { $ff -> create a byte node. }
  1350. { $ffff -> create a word node. }
  1351. { $ffffffff -> create a dword node. }
  1352. {*********************************************************************}
  1353. var
  1354. rangelo,rangehi : int64;
  1355. Begin
  1356. case constsize of
  1357. 1 :
  1358. begin
  1359. p.concat(Tai_const.Create_8bit(byte(value)));
  1360. rangelo:=low(shortint);
  1361. rangehi:=high(byte);
  1362. end;
  1363. 2 :
  1364. begin
  1365. p.concat(Tai_const.Create_16bit(word(value)));
  1366. rangelo:=low(smallint);
  1367. rangehi:=high(word);
  1368. end;
  1369. 4 :
  1370. begin
  1371. p.concat(Tai_const.Create_32bit(longint(value)));
  1372. rangelo:=low(longint);
  1373. rangehi:=high(cardinal);
  1374. end;
  1375. 8 :
  1376. begin
  1377. p.concat(Tai_const.Create_64bit(int64(value)));
  1378. rangelo:=0;
  1379. rangehi:=0;
  1380. end;
  1381. else
  1382. internalerror(200405011);
  1383. end;
  1384. { check for out of bounds }
  1385. if (rangelo<>0) and
  1386. ((value>rangehi) or (value<rangelo)) then
  1387. Message(asmr_e_constant_out_of_bounds);
  1388. end;
  1389. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:aint);
  1390. begin
  1391. p.concat(Tai_const.Createname(sym,l));
  1392. end;
  1393. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  1394. {***********************************************************************}
  1395. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1396. { Description: This routine adds the value constant to the current }
  1397. { instruction linked list. }
  1398. { real_typ -> indicates the type of the real data to initialize: }
  1399. { s32real -> create a single node. }
  1400. { s64real -> create a double node. }
  1401. { s80real -> create an extended node. }
  1402. { s64bit -> create a comp node. }
  1403. { f32bit -> create a fixed node. (not used normally) }
  1404. {***********************************************************************}
  1405. Begin
  1406. case real_typ of
  1407. s32real : p.concat(Tai_real_32bit.Create(value));
  1408. s64real :
  1409. {$ifdef ARM}
  1410. if is_double_hilo_swapped then
  1411. p.concat(Tai_real_64bit.Create_hiloswapped(value))
  1412. else
  1413. {$endif ARM}
  1414. p.concat(Tai_real_64bit.Create(value));
  1415. s80real : p.concat(Tai_real_80bit.Create(value));
  1416. s64comp : p.concat(Tai_comp_64bit.Create(trunc(value)));
  1417. end;
  1418. end;
  1419. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  1420. {*********************************************************************}
  1421. { PROCEDURE ConcatLabel }
  1422. { Description: This routine either emits a label or a labeled }
  1423. { instruction to the linked list of instructions. }
  1424. {*********************************************************************}
  1425. begin
  1426. p.concat(Tai_label.Create(l));
  1427. end;
  1428. procedure ConcatAlign(p:TAsmList;l:aint);
  1429. {*********************************************************************}
  1430. { PROCEDURE ConcatPublic }
  1431. { Description: This routine emits an global definition to the }
  1432. { linked list of instructions.(used by AT&T styled asm) }
  1433. {*********************************************************************}
  1434. begin
  1435. p.concat(Tai_align.Create(l));
  1436. end;
  1437. procedure ConcatPublic(p:TAsmList;const s : string);
  1438. {*********************************************************************}
  1439. { PROCEDURE ConcatPublic }
  1440. { Description: This routine emits an global definition to the }
  1441. { linked list of instructions.(used by AT&T styled asm) }
  1442. {*********************************************************************}
  1443. begin
  1444. p.concat(Tai_symbol.Createname_global(s,AT_LABEL,0));
  1445. end;
  1446. procedure ConcatLocal(p:TAsmList;const s : string);
  1447. {*********************************************************************}
  1448. { PROCEDURE ConcatLocal }
  1449. { Description: This routine emits an local definition to the }
  1450. { linked list of instructions. }
  1451. {*********************************************************************}
  1452. begin
  1453. p.concat(Tai_symbol.Createname(s,AT_LABEL,0));
  1454. end;
  1455. end.