rautils.pas 47 KB

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