2
0

rautils.pas 46 KB

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