rautils.pas 48 KB

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