rautils.pas 46 KB

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