rautils.pas 48 KB

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