rautils.pas 48 KB

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