rautils.pas 49 KB

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