rautils.pas 48 KB

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