rautils.pas 48 KB

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