2
0

rautils.pas 47 KB

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