rautils.pas 45 KB

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