2
0

rautils.pas 45 KB

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