rautils.pas 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  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,
  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);
  53. TOprRec = record
  54. case typ:TOprType of
  55. OPR_NONE : ();
  56. OPR_CONSTANT : (val:longint);
  57. OPR_SYMBOL : (symbol:tasmsymbol;symofs:longint);
  58. OPR_REFERENCE : (ref:treference);
  59. OPR_LOCAL : (localsym:tvarsym;localsymofs:longint;localindexreg:tregister;localscale:byte;localgetoffset:boolean);
  60. OPR_REGISTER : (reg:tregister);
  61. {$ifdef m68k}
  62. OPR_REGLIST : (reglist:Tsupregset);
  63. {$endif m68k}
  64. {$ifdef powerpc}
  65. OPR_COND : (cond : tasmcond);
  66. {$endif powerpc}
  67. {$ifdef arm}
  68. OPR_REGSET : (regset : tcpuregisterset);
  69. {$endif arm}
  70. end;
  71. TOperand = class
  72. hastype, { if the operand has typecasted variable }
  73. hasvar : boolean; { if the operand is loaded with a variable }
  74. size : TCGSize;
  75. opr : TOprRec;
  76. constructor create;virtual;
  77. destructor destroy;override;
  78. Procedure SetSize(_size:longint;force:boolean);virtual;
  79. Procedure SetCorrectSize(opcode:tasmop);virtual;
  80. Function SetupResult:boolean;virtual;
  81. Function SetupSelf:boolean;
  82. Function SetupOldEBP:boolean;
  83. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  84. Function SetupDirectVar(const hs:string): 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. Procedure BuildOpcode;virtual;abstract;
  97. { converts the instruction to an instruction how it's used by the assembler writer
  98. and concats it to the passed list, the newly created item is returned }
  99. function ConcatInstruction(p:TAAsmoutput) : tai;virtual;
  100. Procedure Swapoperands;
  101. end;
  102. tstr2opentry = class(Tnamedindexitem)
  103. op: TAsmOp;
  104. end;
  105. {---------------------------------------------------------------------}
  106. { Expression parser types }
  107. {---------------------------------------------------------------------}
  108. TExprOperator = record
  109. ch: char; { operator }
  110. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  111. end;
  112. String15 = String[15];
  113. {**********************************************************************}
  114. { The following operators are supported: }
  115. { '+' : addition }
  116. { '-' : subtraction }
  117. { '*' : multiplication }
  118. { '/' : modulo division }
  119. { '^' : exclusive or }
  120. { '<' : shift left }
  121. { '>' : shift right }
  122. { '&' : bitwise and }
  123. { '|' : bitwise or }
  124. { '~' : bitwise complement }
  125. { '%' : modulo division }
  126. { nnn: longint numbers }
  127. { ( and ) parenthesis }
  128. {**********************************************************************}
  129. TExprParse = class
  130. public
  131. Constructor create;
  132. Destructor Destroy;override;
  133. Function Evaluate(Expr: String): longint;
  134. Function Priority(_Operator: Char): longint;
  135. private
  136. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  137. RPNTop : longint;
  138. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  139. OpTop : longint;
  140. Procedure RPNPush(Num: Longint);
  141. Function RPNPop: Longint;
  142. Procedure RPNCalc(token: String15; prefix: boolean);
  143. Procedure OpPush(_Operator: char; prefix: boolean);
  144. { In reality returns TExprOperaotr }
  145. Procedure OpPop(var _Operator:TExprOperator);
  146. end;
  147. { Evaluate an expression string to a longint }
  148. Function CalculateExpression(const expression: string): longint;
  149. {---------------------------------------------------------------------}
  150. { String routines }
  151. {---------------------------------------------------------------------}
  152. Function ValDecimal(const S:String):longint;
  153. Function ValOctal(const S:String):longint;
  154. Function ValBinary(const S:String):longint;
  155. Function ValHexaDecimal(const S:String):longint;
  156. Function PadZero(Var s: String; n: byte): Boolean;
  157. Function EscapeToPascal(const s:string): string;
  158. {---------------------------------------------------------------------
  159. Symbol helper routines
  160. ---------------------------------------------------------------------}
  161. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  162. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  163. Function SearchType(const hs:string;var size:longint): Boolean;
  164. Function SearchRecordType(const s:string): boolean;
  165. Function SearchIConstant(const s:string; var l:longint): boolean;
  166. {---------------------------------------------------------------------
  167. Instruction generation routines
  168. ---------------------------------------------------------------------}
  169. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  170. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  171. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  172. Procedure ConcatConstant(p : TAAsmoutput;value: longint; maxvalue: longint);
  173. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  174. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  175. Procedure ConcatString(p : TAAsmoutput;s:string);
  176. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  177. Procedure ConcatPublic(p:TAAsmoutput;const s : string);
  178. Procedure ConcatLocal(p:TAAsmoutput;const s : string);
  179. Procedure ConcatGlobalBss(const s : string;size : longint);
  180. Procedure ConcatLocalBss(const s : string;size : longint);
  181. Implementation
  182. uses
  183. {$ifdef delphi}
  184. sysutils,
  185. {$else}
  186. strings,
  187. {$endif}
  188. defutil,systems,verbose,globals,
  189. symtable,paramgr,
  190. aasmcpu,
  191. procinfo;
  192. {*************************************************************************
  193. TExprParse
  194. *************************************************************************}
  195. Constructor TExprParse.create;
  196. Begin
  197. end;
  198. Procedure TExprParse.RPNPush(Num : longint);
  199. { Add an operand to the top of the RPN stack }
  200. begin
  201. if RPNTop < RPNMax then
  202. begin
  203. Inc(RPNTop);
  204. RPNStack[RPNTop]:=Num;
  205. end
  206. else
  207. Message(asmr_e_expr_illegal);
  208. end;
  209. Function TExprParse.RPNPop : longint;
  210. { Get the operand at the top of the RPN stack }
  211. begin
  212. if RPNTop > 0 then
  213. begin
  214. RPNPop:=RPNStack[RPNTop];
  215. Dec(RPNTop);
  216. end
  217. else
  218. Message(asmr_e_expr_illegal);
  219. end;
  220. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  221. Var
  222. Temp : longint;
  223. n1,n2 : longint;
  224. LocalError : Integer;
  225. begin
  226. { Handle operators }
  227. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  228. Case Token[1] of
  229. '+' :
  230. Begin
  231. if not prefix then
  232. RPNPush(RPNPop + RPNPop);
  233. end;
  234. '-' :
  235. Begin
  236. if prefix then
  237. RPNPush(-(RPNPop))
  238. else
  239. begin
  240. n1:=RPNPop;
  241. n2:=RPNPop;
  242. RPNPush(n2 - n1);
  243. end;
  244. end;
  245. '*' : RPNPush(RPNPop * RPNPop);
  246. '&' :
  247. begin
  248. n1:=RPNPop;
  249. n2:=RPNPop;
  250. RPNPush(n2 and n1);
  251. end;
  252. '|' :
  253. begin
  254. n1:=RPNPop;
  255. n2:=RPNPop;
  256. RPNPush(n2 or n1);
  257. end;
  258. '~' : RPNPush(NOT RPNPop);
  259. '<' :
  260. begin
  261. n1:=RPNPop;
  262. n2:=RPNPop;
  263. RPNPush(n2 SHL n1);
  264. end;
  265. '>' :
  266. begin
  267. n1:=RPNPop;
  268. n2:=RPNPop;
  269. RPNPush(n2 SHR n1);
  270. end;
  271. '%' :
  272. begin
  273. Temp:=RPNPop;
  274. if Temp <> 0 then
  275. RPNPush(RPNPop mod Temp)
  276. else
  277. begin
  278. Message(asmr_e_expr_zero_divide);
  279. { push 1 for error recovery }
  280. RPNPush(1);
  281. end;
  282. end;
  283. '^' : RPNPush(RPNPop XOR RPNPop);
  284. '/' :
  285. begin
  286. Temp:=RPNPop;
  287. if Temp <> 0 then
  288. RPNPush(RPNPop div Temp)
  289. else
  290. begin
  291. Message(asmr_e_expr_zero_divide);
  292. { push 1 for error recovery }
  293. RPNPush(1);
  294. end;
  295. end;
  296. end
  297. else
  298. begin
  299. { Convert String to number and add to stack }
  300. Val(Token, Temp, LocalError);
  301. if LocalError = 0 then
  302. RPNPush(Temp)
  303. else
  304. begin
  305. Message(asmr_e_expr_illegal);
  306. { push 1 for error recovery }
  307. RPNPush(1);
  308. end;
  309. end;
  310. end;
  311. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  312. { Add an operator onto top of the stack }
  313. begin
  314. if OpTop < OpMax then
  315. begin
  316. Inc(OpTop);
  317. OpStack[OpTop].ch:=_Operator;
  318. OpStack[OpTop].is_prefix:=prefix;
  319. end
  320. else
  321. Message(asmr_e_expr_illegal);
  322. end;
  323. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  324. { Get operator at the top of the stack }
  325. begin
  326. if OpTop > 0 then
  327. begin
  328. _Operator:=OpStack[OpTop];
  329. Dec(OpTop);
  330. end
  331. else
  332. Message(asmr_e_expr_illegal);
  333. end;
  334. Function TExprParse.Priority(_Operator : Char) : longint;
  335. { Return priority of operator }
  336. { The greater the priority, the higher the precedence }
  337. begin
  338. Case _Operator OF
  339. '(' :
  340. Priority:=0;
  341. '+', '-' :
  342. Priority:=1;
  343. '*', '/','%','<','>' :
  344. Priority:=2;
  345. '|','&','^','~' :
  346. Priority:=0;
  347. else
  348. Message(asmr_e_expr_illegal);
  349. end;
  350. end;
  351. Function TExprParse.Evaluate(Expr : String):longint;
  352. Var
  353. I : LongInt;
  354. Token : String15;
  355. opr : TExprOperator;
  356. begin
  357. Evaluate:=0;
  358. { Reset stacks }
  359. OpTop :=0;
  360. RPNTop:=0;
  361. Token :='';
  362. { nothing to do ? }
  363. if Expr='' then
  364. exit;
  365. For I:=1 to Length(Expr) DO
  366. begin
  367. if Expr[I] in ['0'..'9'] then
  368. begin { Build multi-digit numbers }
  369. Token:=Token + Expr[I];
  370. if I = Length(Expr) then { Send last one to calculator }
  371. RPNCalc(Token,false);
  372. end
  373. else
  374. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  375. begin
  376. if Token <> '' then
  377. begin { Send last built number to calc. }
  378. RPNCalc(Token,false);
  379. Token:='';
  380. end;
  381. Case Expr[I] OF
  382. '(' : OpPush('(',false);
  383. ')' : begin
  384. While OpStack[OpTop].ch <> '(' DO
  385. Begin
  386. OpPop(opr);
  387. RPNCalc(opr.ch,opr.is_prefix);
  388. end;
  389. OpPop(opr); { Pop off and ignore the '(' }
  390. end;
  391. '+','-','~' : Begin
  392. { workaround for -2147483648 }
  393. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  394. begin
  395. token:='-';
  396. expr[i]:='+';
  397. end;
  398. { if start of expression then surely a prefix }
  399. { or if previous char was also an operator }
  400. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  401. OpPush(Expr[I],true)
  402. else
  403. Begin
  404. { Evaluate all higher priority operators }
  405. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  406. Begin
  407. OpPop(opr);
  408. RPNCalc(opr.ch,opr.is_prefix);
  409. end;
  410. OpPush(Expr[I],false);
  411. End;
  412. end;
  413. '*', '/',
  414. '^','|','&',
  415. '%','<','>' : begin
  416. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  417. Begin
  418. OpPop(opr);
  419. RPNCalc(opr.ch,opr.is_prefix);
  420. end;
  421. OpPush(Expr[I],false);
  422. end;
  423. end; { Case }
  424. end
  425. else
  426. Message(asmr_e_expr_illegal); { Handle bad input error }
  427. end;
  428. { Pop off the remaining operators }
  429. While OpTop > 0 do
  430. Begin
  431. OpPop(opr);
  432. RPNCalc(opr.ch,opr.is_prefix);
  433. end;
  434. { The result is stored on the top of the stack }
  435. Evaluate:=RPNPop;
  436. end;
  437. Destructor TExprParse.Destroy;
  438. Begin
  439. end;
  440. Function CalculateExpression(const expression: string): longint;
  441. var
  442. expr: TExprParse;
  443. Begin
  444. expr:=TExprParse.create;
  445. CalculateExpression:=expr.Evaluate(expression);
  446. expr.Free;
  447. end;
  448. {*************************************************************************}
  449. { String conversions/utils }
  450. {*************************************************************************}
  451. Function EscapeToPascal(const s:string): string;
  452. { converts a C styled string - which contains escape }
  453. { characters to a pascal style string. }
  454. var
  455. i,len : longint;
  456. hs : string;
  457. temp : string;
  458. c : char;
  459. Begin
  460. hs:='';
  461. len:=0;
  462. i:=0;
  463. while (i<length(s)) and (len<255) do
  464. begin
  465. Inc(i);
  466. if (s[i]='\') and (i<length(s)) then
  467. Begin
  468. inc(i);
  469. case s[i] of
  470. '\' :
  471. c:='\';
  472. 'b':
  473. c:=#8;
  474. 'f':
  475. c:=#12;
  476. 'n':
  477. c:=#10;
  478. 'r':
  479. c:=#13;
  480. 't':
  481. c:=#9;
  482. '"':
  483. c:='"';
  484. '0'..'7':
  485. Begin
  486. temp:=s[i];
  487. temp:=temp+s[i+1];
  488. temp:=temp+s[i+2];
  489. inc(i,2);
  490. c:=chr(ValOctal(temp));
  491. end;
  492. 'x':
  493. Begin
  494. temp:=s[i+1];
  495. temp:=temp+s[i+2];
  496. inc(i,2);
  497. c:=chr(ValHexaDecimal(temp));
  498. end;
  499. else
  500. Begin
  501. Message1(asmr_e_escape_seq_ignored,s[i]);
  502. c:=s[i];
  503. end;
  504. end;
  505. end
  506. else
  507. c:=s[i];
  508. inc(len);
  509. hs[len]:=c;
  510. end;
  511. hs[0]:=chr(len);
  512. EscapeToPascal:=hs;
  513. end;
  514. Function ValDecimal(const S:String):longint;
  515. { Converts a decimal string to longint }
  516. var
  517. vs,c : longint;
  518. Begin
  519. vs:=0;
  520. for c:=1 to length(s) do
  521. begin
  522. vs:=vs*10;
  523. if s[c] in ['0'..'9'] then
  524. inc(vs,ord(s[c])-ord('0'))
  525. else
  526. begin
  527. Message1(asmr_e_error_converting_decimal,s);
  528. ValDecimal:=0;
  529. exit;
  530. end;
  531. end;
  532. ValDecimal:=vs;
  533. end;
  534. Function ValOctal(const S:String):longint;
  535. { Converts an octal string to longint }
  536. var
  537. vs,c : longint;
  538. Begin
  539. vs:=0;
  540. for c:=1 to length(s) do
  541. begin
  542. vs:=vs shl 3;
  543. if s[c] in ['0'..'7'] then
  544. inc(vs,ord(s[c])-ord('0'))
  545. else
  546. begin
  547. Message1(asmr_e_error_converting_octal,s);
  548. ValOctal:=0;
  549. exit;
  550. end;
  551. end;
  552. ValOctal:=vs;
  553. end;
  554. Function ValBinary(const S:String):longint;
  555. { Converts a binary string to longint }
  556. var
  557. vs,c : longint;
  558. Begin
  559. vs:=0;
  560. for c:=1 to length(s) do
  561. begin
  562. vs:=vs shl 1;
  563. if s[c] in ['0'..'1'] then
  564. inc(vs,ord(s[c])-ord('0'))
  565. else
  566. begin
  567. Message1(asmr_e_error_converting_binary,s);
  568. ValBinary:=0;
  569. exit;
  570. end;
  571. end;
  572. ValBinary:=vs;
  573. end;
  574. Function ValHexadecimal(const S:String):longint;
  575. { Converts a binary string to longint }
  576. var
  577. vs,c : longint;
  578. Begin
  579. vs:=0;
  580. for c:=1 to length(s) do
  581. begin
  582. vs:=vs shl 4;
  583. case s[c] of
  584. '0'..'9' :
  585. inc(vs,ord(s[c])-ord('0'));
  586. 'A'..'F' :
  587. inc(vs,ord(s[c])-ord('A')+10);
  588. 'a'..'f' :
  589. inc(vs,ord(s[c])-ord('a')+10);
  590. else
  591. begin
  592. Message1(asmr_e_error_converting_hexadecimal,s);
  593. ValHexadecimal:=0;
  594. exit;
  595. end;
  596. end;
  597. end;
  598. ValHexadecimal:=vs;
  599. end;
  600. Function PadZero(Var s: String; n: byte): Boolean;
  601. Begin
  602. PadZero:=TRUE;
  603. { Do some error checking first }
  604. if Length(s) = n then
  605. exit
  606. else
  607. if Length(s) > n then
  608. Begin
  609. PadZero:=FALSE;
  610. delete(s,n+1,length(s));
  611. exit;
  612. end
  613. else
  614. PadZero:=TRUE;
  615. { Fill it up with the specified character }
  616. fillchar(s[length(s)+1],n-1,#0);
  617. s[0]:=chr(n);
  618. end;
  619. {****************************************************************************
  620. TOperand
  621. ****************************************************************************}
  622. constructor TOperand.Create;
  623. begin
  624. size:=OS_NO;
  625. hastype:=false;
  626. hasvar:=false;
  627. FillChar(Opr,sizeof(Opr),0);
  628. end;
  629. destructor TOperand.destroy;
  630. begin
  631. end;
  632. Procedure TOperand.SetSize(_size:longint;force:boolean);
  633. begin
  634. if force or
  635. ((size = OS_NO) and (_size<=extended_size)) then
  636. Begin
  637. case _size of
  638. 1 : size:=OS_8;
  639. 2 : size:=OS_16{ could be S_IS};
  640. 4 : size:=OS_32{ could be S_IL or S_FS};
  641. 8 : size:=OS_64{ could be S_D or S_FL};
  642. else
  643. begin
  644. { extended_size can also be 8, resulting in a
  645. duplicate label }
  646. if _size=extended_size then
  647. size:=OS_F80;
  648. end;
  649. end;
  650. end;
  651. end;
  652. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  653. begin
  654. end;
  655. Function TOperand.SetupResult:boolean;
  656. Begin
  657. SetupResult:=false;
  658. { replace by correct offset. }
  659. if (not is_void(current_procinfo.procdef.rettype.def)) then
  660. begin
  661. if (m_tp7 in aktmodeswitches) and
  662. (not paramanager.ret_in_param(current_procinfo.procdef.rettype.def,current_procinfo.procdef.proccalloption)) then
  663. begin
  664. Message(asmr_e_cannot_use_RESULT_here);
  665. exit;
  666. end;
  667. SetupResult:=setupvar('result',false)
  668. end
  669. else
  670. Message(asmr_e_void_function);
  671. end;
  672. Function TOperand.SetupSelf:boolean;
  673. Begin
  674. SetupSelf:=false;
  675. if assigned(current_procinfo.procdef._class) then
  676. SetupSelf:=setupvar('self',false)
  677. else
  678. Message(asmr_e_cannot_use_SELF_outside_a_method);
  679. end;
  680. Function TOperand.SetupOldEBP:boolean;
  681. Begin
  682. SetupOldEBP:=false;
  683. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  684. SetupOldEBP:=setupvar('parentframe',false)
  685. else
  686. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  687. end;
  688. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  689. function symtable_has_varsyms(st:tsymtable):boolean;
  690. var
  691. sym : tsym;
  692. begin
  693. result:=false;
  694. sym:=tsym(st.symindex.first);
  695. while assigned(sym) do
  696. begin
  697. if sym.typ=varsym then
  698. begin
  699. result:=true;
  700. exit;
  701. end;
  702. sym:=tsym(sym.indexnext);
  703. end;
  704. end;
  705. procedure setconst(l:longint);
  706. begin
  707. { We return the address of the field, just like Delphi/TP }
  708. case opr.typ of
  709. OPR_NONE :
  710. begin
  711. opr.typ:=OPR_CONSTANT;
  712. opr.val:=l;
  713. end;
  714. OPR_CONSTANT :
  715. inc(opr.val,l);
  716. OPR_REFERENCE :
  717. inc(opr.ref.offset,l);
  718. OPR_LOCAL :
  719. inc(opr.localsymofs,l);
  720. else
  721. Message(asmr_e_invalid_operand_type);
  722. end;
  723. end;
  724. { search and sets up the correct fields in the Instr record }
  725. { for the NON-constant identifier passed to the routine. }
  726. { if not found returns FALSE. }
  727. var
  728. sym : tsym;
  729. srsymtable : tsymtable;
  730. harrdef : tarraydef;
  731. indexreg : tregister;
  732. l : longint;
  733. plist : psymlistitem;
  734. Begin
  735. SetupVar:=false;
  736. asmsearchsym(s,sym,srsymtable);
  737. if sym = nil then
  738. exit;
  739. if sym.typ=absolutesym then
  740. begin
  741. if (tabsolutesym(sym).abstyp=tovar) then
  742. begin
  743. { Only support simple loads }
  744. plist:=tabsolutesym(sym).ref.firstsym;
  745. if assigned(plist) and
  746. (plist^.sltype=sl_load) then
  747. sym:=plist^.sym
  748. else
  749. begin
  750. Message(asmr_e_unsupported_symbol_type);
  751. exit;
  752. end;
  753. end
  754. else
  755. begin
  756. Message(asmr_e_unsupported_symbol_type);
  757. exit;
  758. end;
  759. end;
  760. case sym.typ of
  761. varsym :
  762. begin
  763. { we always assume in asm statements that }
  764. { that the variable is valid. }
  765. tvarsym(sym).varstate:=vs_used;
  766. inc(tvarsym(sym).refs);
  767. case tvarsym(sym).owner.symtabletype of
  768. objectsymtable :
  769. begin
  770. setconst(tvarsym(sym).fieldoffset);
  771. hasvar:=true;
  772. SetupVar:=true;
  773. Exit;
  774. end;
  775. globalsymtable,
  776. staticsymtable :
  777. begin
  778. initref;
  779. opr.ref.symbol:=objectlibrary.newasmsymboldata(tvarsym(sym).mangledname);
  780. end;
  781. parasymtable,
  782. localsymtable :
  783. begin
  784. if (vo_is_external in tvarsym(sym).varoptions) then
  785. begin
  786. initref;
  787. opr.ref.symbol:=objectlibrary.newasmsymboldata(tvarsym(sym).mangledname)
  788. end
  789. else
  790. begin
  791. if opr.typ=OPR_REFERENCE then
  792. begin
  793. indexreg:=opr.ref.base;
  794. if opr.ref.index<>NR_NO then
  795. begin
  796. if indexreg=NR_NO then
  797. indexreg:=opr.ref.index
  798. else
  799. Message(asmr_e_multiple_index);
  800. end;
  801. end
  802. else
  803. indexreg:=NR_NO;
  804. opr.typ:=OPR_LOCAL;
  805. if assigned(current_procinfo.parent) and
  806. (current_procinfo.procdef.proccalloption<>pocall_inline) and
  807. (tvarsym(sym).owner<>current_procinfo.procdef.localst) and
  808. (tvarsym(sym).owner<>current_procinfo.procdef.parast) and
  809. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  810. symtable_has_varsyms(current_procinfo.procdef.localst) then
  811. message1(asmr_e_local_para_unreachable,s);
  812. opr.localsym:=tvarsym(sym);
  813. opr.localsymofs:=0;
  814. opr.localindexreg:=indexreg;
  815. opr.localscale:=0;
  816. opr.localgetoffset:=GetOffset;
  817. end;
  818. if paramanager.push_addr_param(tvarsym(sym).varspez,tvarsym(sym).vartype.def,current_procinfo.procdef.proccalloption) then
  819. SetSize(pointer_size,false);
  820. end;
  821. end;
  822. case tvarsym(sym).vartype.def.deftype of
  823. orddef,
  824. enumdef,
  825. pointerdef,
  826. floatdef :
  827. SetSize(tvarsym(sym).getsize,false);
  828. arraydef :
  829. begin
  830. { for arrays try to get the element size, take care of
  831. multiple indexes }
  832. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  833. while assigned(harrdef.elementtype.def) and
  834. (harrdef.elementtype.def.deftype=arraydef) do
  835. harrdef:=tarraydef(harrdef.elementtype.def);
  836. SetSize(harrdef.elesize,false);
  837. end;
  838. end;
  839. hasvar:=true;
  840. SetupVar:=true;
  841. Exit;
  842. end;
  843. typedconstsym :
  844. begin
  845. initref;
  846. opr.ref.symbol:=objectlibrary.newasmsymboldata(ttypedconstsym(sym).mangledname);
  847. case ttypedconstsym(sym).typedconsttype.def.deftype of
  848. orddef,
  849. enumdef,
  850. pointerdef,
  851. floatdef :
  852. SetSize(ttypedconstsym(sym).getsize,false);
  853. arraydef :
  854. begin
  855. { for arrays try to get the element size, take care of
  856. multiple indexes }
  857. harrdef:=tarraydef(ttypedconstsym(sym).typedconsttype.def);
  858. while assigned(harrdef.elementtype.def) and
  859. (harrdef.elementtype.def.deftype=arraydef) do
  860. harrdef:=tarraydef(harrdef.elementtype.def);
  861. SetSize(harrdef.elesize,false);
  862. end;
  863. end;
  864. hasvar:=true;
  865. SetupVar:=true;
  866. Exit;
  867. end;
  868. constsym :
  869. begin
  870. if tconstsym(sym).consttyp in [constint,constchar,constbool] then
  871. begin
  872. setconst(tconstsym(sym).value.valueord);
  873. SetupVar:=true;
  874. Exit;
  875. end;
  876. end;
  877. typesym :
  878. begin
  879. if ttypesym(sym).restype.def.deftype in [recorddef,objectdef] then
  880. begin
  881. setconst(0);
  882. SetupVar:=TRUE;
  883. Exit;
  884. end;
  885. end;
  886. procsym :
  887. begin
  888. if opr.typ<>OPR_NONE then
  889. Message(asmr_e_invalid_operand_type);
  890. if Tprocsym(sym).procdef_count>1 then
  891. Message(asmr_w_calling_overload_func);
  892. l:=opr.ref.offset;
  893. opr.typ:=OPR_SYMBOL;
  894. opr.symbol:=objectlibrary.newasmsymbol(tprocsym(sym).first_procdef.mangledname);
  895. opr.symofs:=l;
  896. hasvar:=true;
  897. SetupVar:=TRUE;
  898. Exit;
  899. end;
  900. else
  901. begin
  902. Message(asmr_e_unsupported_symbol_type);
  903. exit;
  904. end;
  905. end;
  906. end;
  907. { looks for internal names of variables and routines }
  908. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  909. var
  910. p : tasmsymbol;
  911. begin
  912. SetupDirectVar:=false;
  913. p:=objectlibrary.getasmsymbol(hs);
  914. if assigned(p) then
  915. begin
  916. InitRef;
  917. opr.ref.symbol:=p;
  918. hasvar:=true;
  919. SetupDirectVar:=true;
  920. end;
  921. end;
  922. procedure TOperand.InitRef;
  923. {*********************************************************************}
  924. { Description: This routine first check if the opcode is of }
  925. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  926. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  927. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  928. { to point to the default segment. }
  929. {*********************************************************************}
  930. var
  931. l : longint;
  932. Begin
  933. case opr.typ of
  934. OPR_REFERENCE :
  935. exit;
  936. OPR_CONSTANT :
  937. begin
  938. l:=opr.val;
  939. opr.typ:=OPR_REFERENCE;
  940. Fillchar(opr.ref,sizeof(treference),0);
  941. opr.Ref.Offset:=l;
  942. end;
  943. OPR_NONE :
  944. begin
  945. opr.typ:=OPR_REFERENCE;
  946. Fillchar(opr.ref,sizeof(treference),0);
  947. end;
  948. else
  949. begin
  950. Message(asmr_e_invalid_operand_type);
  951. { Recover }
  952. opr.typ:=OPR_REFERENCE;
  953. Fillchar(opr.ref,sizeof(treference),0);
  954. end;
  955. end;
  956. end;
  957. {****************************************************************************
  958. TInstruction
  959. ****************************************************************************}
  960. constructor TInstruction.create(optype : tcoperand);
  961. var
  962. i : longint;
  963. Begin
  964. { these field are set to 0 anyways by the constructor helper (FK)
  965. Opcode:=A_NONE;
  966. Condition:=C_NONE;
  967. Ops:=0;
  968. }
  969. for i:=1 to max_operands do
  970. Operands[i]:=optype.create;
  971. Labeled:=false;
  972. end;
  973. destructor TInstruction.destroy;
  974. var
  975. i : longint;
  976. Begin
  977. for i:=1 to max_operands do
  978. Operands[i].free;
  979. end;
  980. Procedure TInstruction.Swapoperands;
  981. Var
  982. p : toperand;
  983. Begin
  984. case Ops of
  985. 2 :
  986. begin
  987. p:=Operands[1];
  988. Operands[1]:=Operands[2];
  989. Operands[2]:=p;
  990. end;
  991. 3 :
  992. begin
  993. p:=Operands[1];
  994. Operands[1]:=Operands[3];
  995. Operands[3]:=p;
  996. end;
  997. end;
  998. end;
  999. function TInstruction.ConcatInstruction(p:TAAsmoutput) : tai;
  1000. var
  1001. ai : taicpu;
  1002. i : longint;
  1003. begin
  1004. ai:=taicpu.op_none(opcode);
  1005. ai.Ops:=Ops;
  1006. ai.Allocate_oper(Ops);
  1007. for i:=1 to Ops do
  1008. begin
  1009. case operands[i].opr.typ of
  1010. OPR_CONSTANT :
  1011. ai.loadconst(i-1,aword(operands[i].opr.val));
  1012. OPR_REGISTER:
  1013. ai.loadreg(i-1,operands[i].opr.reg);
  1014. OPR_SYMBOL:
  1015. ai.loadsymbol(i-1,operands[i].opr.symbol,operands[i].opr.symofs);
  1016. OPR_LOCAL :
  1017. ai.loadlocal(i-1,operands[i].opr.localsym,operands[i].opr.localsymofs,operands[i].opr.localindexreg,
  1018. operands[i].opr.localscale,operands[i].opr.localgetoffset);
  1019. OPR_REFERENCE:
  1020. ai.loadref(i-1,operands[i].opr.ref);
  1021. {$ifdef ARM}
  1022. OPR_REGSET:
  1023. ai.loadregset(i-1,operands[i].opr.regset);
  1024. {$endif ARM}
  1025. end;
  1026. end;
  1027. ai.SetCondition(condition);
  1028. { Concat the opcode or give an error }
  1029. if assigned(ai) then
  1030. p.concat(ai)
  1031. else
  1032. Message(asmr_e_invalid_opcode_and_operand);
  1033. result:=ai;
  1034. end;
  1035. {***************************************************************************
  1036. TLocalLabel
  1037. ***************************************************************************}
  1038. constructor TLocalLabel.create(const n:string);
  1039. begin
  1040. inherited CreateName(n);
  1041. lab:=nil;
  1042. emitted:=false;
  1043. end;
  1044. function TLocalLabel.Gettasmlabel:tasmlabel;
  1045. begin
  1046. if not assigned(lab) then
  1047. begin
  1048. objectlibrary.getlabel(lab);
  1049. { this label is forced to be used so it's always written }
  1050. lab.increfs;
  1051. end;
  1052. Gettasmlabel:=lab;
  1053. end;
  1054. {***************************************************************************
  1055. TLocalLabelList
  1056. ***************************************************************************}
  1057. procedure LocalLabelEmitted(p:tnamedindexitem;arg:pointer);
  1058. begin
  1059. if not TLocalLabel(p).emitted then
  1060. Message1(asmr_e_unknown_label_identifier,p.name);
  1061. end;
  1062. procedure TLocalLabelList.CheckEmitted;
  1063. begin
  1064. ForEach_Static({$ifdef FPCPROCVAR}@{$endif}LocalLabelEmitted,nil)
  1065. end;
  1066. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1067. var
  1068. lab : TLocalLabel;
  1069. Begin
  1070. CreateLocalLabel:=true;
  1071. { Check if it already is defined }
  1072. lab:=TLocalLabel(LocalLabellist.Search(s));
  1073. if not assigned(lab) then
  1074. begin
  1075. lab:=TLocalLabel.Create(s);
  1076. LocalLabellist.Insert(lab);
  1077. end;
  1078. { set emitted flag and check for dup syms }
  1079. if emit then
  1080. begin
  1081. if lab.Emitted then
  1082. begin
  1083. Message1(asmr_e_dup_local_sym,lab.Name);
  1084. CreateLocalLabel:=false;
  1085. end;
  1086. lab.Emitted:=true;
  1087. end;
  1088. hl:=lab.Gettasmlabel;
  1089. end;
  1090. {****************************************************************************
  1091. Symbol table helper routines
  1092. ****************************************************************************}
  1093. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  1094. var
  1095. i : integer;
  1096. begin
  1097. i:=pos('.',s);
  1098. { allow unit.identifier }
  1099. if i>0 then
  1100. begin
  1101. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1102. if assigned(srsym) then
  1103. begin
  1104. if (srsym.typ=unitsym) and
  1105. (srsym.owner.unitid=0) then
  1106. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,Copy(s,i+1,255))
  1107. else
  1108. srsym:=nil;
  1109. end;
  1110. end
  1111. else
  1112. searchsym(s,srsym,srsymtable);
  1113. end;
  1114. Function SearchType(const hs:string;var size:longint): Boolean;
  1115. var
  1116. srsym : tsym;
  1117. srsymtable : tsymtable;
  1118. begin
  1119. result:=false;
  1120. size:=0;
  1121. asmsearchsym(hs,srsym,srsymtable);
  1122. if assigned(srsym) and
  1123. (srsym.typ=typesym) then
  1124. begin
  1125. size:=ttypesym(srsym).restype.def.size;
  1126. result:=true;
  1127. end;
  1128. end;
  1129. Function SearchRecordType(const s:string): boolean;
  1130. var
  1131. srsym : tsym;
  1132. srsymtable : tsymtable;
  1133. Begin
  1134. SearchRecordType:=false;
  1135. { Check the constants in symtable }
  1136. asmsearchsym(s,srsym,srsymtable);
  1137. if srsym <> nil then
  1138. Begin
  1139. case srsym.typ of
  1140. typesym :
  1141. begin
  1142. if ttypesym(srsym).restype.def.deftype in [recorddef,objectdef] then
  1143. begin
  1144. SearchRecordType:=true;
  1145. exit;
  1146. end;
  1147. end;
  1148. end;
  1149. end;
  1150. end;
  1151. Function SearchIConstant(const s:string; var l:longint): boolean;
  1152. {**********************************************************************}
  1153. { Description: Searches for a CONSTANT of name s in either the local }
  1154. { symbol list, then in the global symbol list, and returns the value }
  1155. { of that constant in l. Returns TRUE if successfull, if not found, }
  1156. { or if the constant is not of correct type, then returns FALSE }
  1157. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1158. { respectively. }
  1159. {**********************************************************************}
  1160. var
  1161. srsym : tsym;
  1162. srsymtable : tsymtable;
  1163. Begin
  1164. SearchIConstant:=false;
  1165. { check for TRUE or FALSE reserved words first }
  1166. if s = 'TRUE' then
  1167. Begin
  1168. SearchIConstant:=TRUE;
  1169. l:=1;
  1170. exit;
  1171. end;
  1172. if s = 'FALSE' then
  1173. Begin
  1174. SearchIConstant:=TRUE;
  1175. l:=0;
  1176. exit;
  1177. end;
  1178. { Check the constants in symtable }
  1179. asmsearchsym(s,srsym,srsymtable);
  1180. if srsym <> nil then
  1181. Begin
  1182. case srsym.typ of
  1183. constsym :
  1184. begin
  1185. if (tconstsym(srsym).consttyp in [constord,constint,constchar,constbool]) then
  1186. Begin
  1187. l:=tconstsym(srsym).value.valueord;
  1188. SearchIConstant:=TRUE;
  1189. exit;
  1190. end;
  1191. end;
  1192. enumsym:
  1193. Begin
  1194. l:=tenumsym(srsym).value;
  1195. SearchIConstant:=TRUE;
  1196. exit;
  1197. end;
  1198. end;
  1199. end;
  1200. end;
  1201. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1202. { search and returns the offset and size of records/objects of the base }
  1203. { with field name setup in field. }
  1204. { returns FALSE if not found. }
  1205. { used when base is a variable or a typed constant name. }
  1206. var
  1207. st : tsymtable;
  1208. harrdef : tarraydef;
  1209. sym : tsym;
  1210. srsymtable : tsymtable;
  1211. i : longint;
  1212. base : string;
  1213. Begin
  1214. GetRecordOffsetSize:=FALSE;
  1215. Offset:=0;
  1216. Size:=0;
  1217. i:=pos('.',s);
  1218. if i=0 then
  1219. i:=255;
  1220. base:=Copy(s,1,i-1);
  1221. delete(s,1,i);
  1222. if base='SELF' then
  1223. st:=current_procinfo.procdef._class.symtable
  1224. else
  1225. begin
  1226. asmsearchsym(base,sym,srsymtable);
  1227. st:=nil;
  1228. { we can start with a var,type,typedconst }
  1229. case sym.typ of
  1230. varsym :
  1231. begin
  1232. case tvarsym(sym).vartype.def.deftype of
  1233. recorddef :
  1234. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1235. objectdef :
  1236. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1237. end;
  1238. end;
  1239. typesym :
  1240. begin
  1241. case ttypesym(sym).restype.def.deftype of
  1242. recorddef :
  1243. st:=trecorddef(ttypesym(sym).restype.def).symtable;
  1244. objectdef :
  1245. st:=tobjectdef(ttypesym(sym).restype.def).symtable;
  1246. end;
  1247. end;
  1248. typedconstsym :
  1249. begin
  1250. case ttypedconstsym(sym).typedconsttype.def.deftype of
  1251. recorddef :
  1252. st:=trecorddef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1253. objectdef :
  1254. st:=tobjectdef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1255. end;
  1256. end;
  1257. end;
  1258. end;
  1259. { now walk all recordsymtables }
  1260. while assigned(st) and (s<>'') do
  1261. begin
  1262. { load next field in base }
  1263. i:=pos('.',s);
  1264. if i=0 then
  1265. i:=255;
  1266. base:=Copy(s,1,i-1);
  1267. delete(s,1,i);
  1268. if st.symtabletype=objectsymtable then
  1269. sym:=search_class_member(tobjectdef(st.defowner),base)
  1270. else
  1271. sym:=tsym(st.search(base));
  1272. if not assigned(sym) then
  1273. begin
  1274. GetRecordOffsetSize:=false;
  1275. exit;
  1276. end;
  1277. st:=nil;
  1278. case sym.typ of
  1279. varsym :
  1280. begin
  1281. inc(Offset,tvarsym(sym).fieldoffset);
  1282. Size:=tvarsym(sym).getsize;
  1283. case tvarsym(sym).vartype.def.deftype of
  1284. arraydef :
  1285. begin
  1286. { for arrays try to get the element size, take care of
  1287. multiple indexes }
  1288. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  1289. while assigned(harrdef.elementtype.def) and
  1290. (harrdef.elementtype.def.deftype=arraydef) do
  1291. harrdef:=tarraydef(harrdef.elementtype.def);
  1292. size:=harrdef.elesize;
  1293. end;
  1294. recorddef :
  1295. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1296. objectdef :
  1297. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1298. end;
  1299. end;
  1300. end;
  1301. end;
  1302. GetRecordOffsetSize:=(s='');
  1303. end;
  1304. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1305. var
  1306. sym : tsym;
  1307. srsymtable : tsymtable;
  1308. hs : string;
  1309. Begin
  1310. hl:=nil;
  1311. SearchLabel:=false;
  1312. { Check for pascal labels, which are case insensetive }
  1313. hs:=upper(s);
  1314. asmsearchsym(hs,sym,srsymtable);
  1315. if sym=nil then
  1316. exit;
  1317. case sym.typ of
  1318. labelsym :
  1319. begin
  1320. hl:=tlabelsym(sym).lab;
  1321. if emit then
  1322. tlabelsym(sym).defined:=true
  1323. else
  1324. tlabelsym(sym).used:=true;
  1325. SearchLabel:=true;
  1326. exit;
  1327. end;
  1328. end;
  1329. end;
  1330. {*************************************************************************}
  1331. { Instruction Generation Utilities }
  1332. {*************************************************************************}
  1333. Procedure ConcatString(p : TAAsmoutput;s:string);
  1334. {*********************************************************************}
  1335. { PROCEDURE ConcatString(s:string); }
  1336. { Description: This routine adds the character chain pointed to in }
  1337. { s to the instruction linked list. }
  1338. {*********************************************************************}
  1339. Var
  1340. pc: PChar;
  1341. Begin
  1342. getmem(pc,length(s)+1);
  1343. p.concat(Tai_string.Create_length_pchar(strpcopy(pc,s),length(s)));
  1344. end;
  1345. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  1346. {*********************************************************************}
  1347. { PROCEDURE ConcatPasString(s:string); }
  1348. { Description: This routine adds the character chain pointed to in }
  1349. { s to the instruction linked list, contrary to ConcatString it }
  1350. { uses a pascal style string, so it conserves null characters. }
  1351. {*********************************************************************}
  1352. Begin
  1353. p.concat(Tai_string.Create(s));
  1354. end;
  1355. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  1356. {*********************************************************************}
  1357. { PROCEDURE ConcatDirect(s:string) }
  1358. { Description: This routine output the string directly to the asm }
  1359. { output, it is only sed when writing special labels in AT&T mode, }
  1360. { and should not be used without due consideration, since it may }
  1361. { cause problems. }
  1362. {*********************************************************************}
  1363. Var
  1364. pc: PChar;
  1365. Begin
  1366. getmem(pc,length(s)+1);
  1367. p.concat(Tai_direct.Create(strpcopy(pc,s)));
  1368. end;
  1369. Procedure ConcatConstant(p: TAAsmoutput; value: longint; maxvalue: longint);
  1370. {*********************************************************************}
  1371. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1372. { Description: This routine adds the value constant to the current }
  1373. { instruction linked list. }
  1374. { maxvalue -> indicates the size of the data to initialize: }
  1375. { $ff -> create a byte node. }
  1376. { $ffff -> create a word node. }
  1377. { $ffffffff -> create a dword node. }
  1378. {*********************************************************************}
  1379. Begin
  1380. if (maxvalue <> longint($ffffffff)) and (value > maxvalue) then
  1381. Begin
  1382. Message(asmr_e_constant_out_of_bounds);
  1383. { assuming a value of maxvalue }
  1384. value:=maxvalue;
  1385. end;
  1386. if maxvalue = $ff then
  1387. p.concat(Tai_const.Create_8bit(byte(value)))
  1388. else
  1389. if maxvalue = $ffff then
  1390. p.concat(Tai_const.Create_16bit(word(value)))
  1391. else
  1392. if maxvalue = longint($ffffffff) then
  1393. p.concat(Tai_const.Create_32bit(longint(value)));
  1394. end;
  1395. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  1396. begin
  1397. p.concat(Tai_const_symbol.Createname_offset(sym,l));
  1398. end;
  1399. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  1400. {***********************************************************************}
  1401. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1402. { Description: This routine adds the value constant to the current }
  1403. { instruction linked list. }
  1404. { real_typ -> indicates the type of the real data to initialize: }
  1405. { s32real -> create a single node. }
  1406. { s64real -> create a double node. }
  1407. { s80real -> create an extended node. }
  1408. { s64bit -> create a comp node. }
  1409. { f32bit -> create a fixed node. (not used normally) }
  1410. {***********************************************************************}
  1411. Begin
  1412. case real_typ of
  1413. s32real : p.concat(Tai_real_32bit.Create(value));
  1414. s64real : p.concat(Tai_real_64bit.Create(value));
  1415. s80real : p.concat(Tai_real_80bit.Create(value));
  1416. s64comp : p.concat(Tai_comp_64bit.Create(trunc(value)));
  1417. end;
  1418. end;
  1419. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  1420. {*********************************************************************}
  1421. { PROCEDURE ConcatLabel }
  1422. { Description: This routine either emits a label or a labeled }
  1423. { instruction to the linked list of instructions. }
  1424. {*********************************************************************}
  1425. begin
  1426. p.concat(Tai_label.Create(l));
  1427. end;
  1428. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  1429. {*********************************************************************}
  1430. { PROCEDURE ConcatPublic }
  1431. { Description: This routine emits an global definition to the }
  1432. { linked list of instructions.(used by AT&T styled asm) }
  1433. {*********************************************************************}
  1434. begin
  1435. p.concat(Tai_align.Create(l));
  1436. end;
  1437. procedure ConcatPublic(p:TAAsmoutput;const s : string);
  1438. {*********************************************************************}
  1439. { PROCEDURE ConcatPublic }
  1440. { Description: This routine emits an global definition to the }
  1441. { linked list of instructions.(used by AT&T styled asm) }
  1442. {*********************************************************************}
  1443. begin
  1444. p.concat(Tai_symbol.Createname_global(s,0));
  1445. end;
  1446. procedure ConcatLocal(p:TAAsmoutput;const s : string);
  1447. {*********************************************************************}
  1448. { PROCEDURE ConcatLocal }
  1449. { Description: This routine emits an local definition to the }
  1450. { linked list of instructions. }
  1451. {*********************************************************************}
  1452. begin
  1453. p.concat(Tai_symbol.Createname(s,0));
  1454. end;
  1455. Procedure ConcatGlobalBss(const s : string;size : longint);
  1456. {*********************************************************************}
  1457. { PROCEDURE ConcatGlobalBss }
  1458. { Description: This routine emits an global datablock to the }
  1459. { linked list of instructions. }
  1460. {*********************************************************************}
  1461. begin
  1462. bssSegment.concat(Tai_datablock.Create_global(s,size));
  1463. end;
  1464. Procedure ConcatLocalBss(const s : string;size : longint);
  1465. {*********************************************************************}
  1466. { PROCEDURE ConcatLocalBss }
  1467. { Description: This routine emits a local datablcok to the }
  1468. { linked list of instructions. }
  1469. {*********************************************************************}
  1470. begin
  1471. bssSegment.concat(Tai_datablock.Create(s,size));
  1472. end;
  1473. end.
  1474. {
  1475. $Log$
  1476. Revision 1.80 2003-11-30 10:15:42 jonas
  1477. * fixed compilation for non-x86
  1478. Revision 1.79 2003/11/21 16:29:26 florian
  1479. * fixed reading of reg. sets in the arm assembler reader
  1480. Revision 1.78 2003/11/17 23:23:47 florian
  1481. + first part of arm assembler reader
  1482. Revision 1.77 2003/11/12 16:05:39 florian
  1483. * assembler readers OOPed
  1484. + typed currency constants
  1485. + typed 128 bit float constants if the CPU supports it
  1486. Revision 1.76 2003/10/30 19:59:00 peter
  1487. * support scalefactor for opr_local
  1488. * support reference with opr_local set, fixes tw2631
  1489. Revision 1.75 2003/10/29 16:47:18 peter
  1490. * fix field offset in reference
  1491. Revision 1.74 2003/10/29 15:40:20 peter
  1492. * support indexing and offset retrieval for locals
  1493. Revision 1.73 2003/10/28 15:36:01 peter
  1494. * absolute to object field supported, fixes tb0458
  1495. Revision 1.72 2003/10/24 17:39:03 peter
  1496. * more intel parser updates
  1497. Revision 1.71 2003/10/23 17:19:11 peter
  1498. * SearchType returns also the size
  1499. Revision 1.70 2003/10/08 19:39:58 peter
  1500. * allow access to parent locals when the currnet localst has no
  1501. varsyms
  1502. Revision 1.69 2003/10/01 20:34:49 peter
  1503. * procinfo unit contains tprocinfo
  1504. * cginfo renamed to cgbase
  1505. * moved cgmessage to verbose
  1506. * fixed ppc and sparc compiles
  1507. Revision 1.68 2003/09/25 14:57:36 peter
  1508. * fix check for unreachable locals
  1509. Revision 1.67 2003/09/23 17:56:06 peter
  1510. * locals and paras are allocated in the code generation
  1511. * tvarsym.localloc contains the location of para/local when
  1512. generating code for the current procedure
  1513. Revision 1.66 2003/09/16 16:17:01 peter
  1514. * varspez in calls to push_addr_param
  1515. Revision 1.65 2003/09/03 15:55:01 peter
  1516. * NEWRA branch merged
  1517. Revision 1.64.2.1 2003/08/27 19:55:54 peter
  1518. * first tregister patch
  1519. Revision 1.64 2003/06/13 21:19:31 peter
  1520. * current_procdef removed, use current_procinfo.procdef instead
  1521. Revision 1.63 2003/06/06 14:43:29 peter
  1522. * absolutesym support
  1523. Revision 1.62 2003/05/30 23:57:08 peter
  1524. * more sparc cleanup
  1525. * accumulator removed, splitted in function_return_reg (called) and
  1526. function_result_reg (caller)
  1527. Revision 1.61 2003/05/25 08:55:49 peter
  1528. * load result using hidden parameter
  1529. Revision 1.60 2003/05/15 18:58:53 peter
  1530. * removed selfpointer_offset, vmtpointer_offset
  1531. * tvarsym.adjusted_address
  1532. * address in localsymtable is now in the real direction
  1533. * removed some obsolete globals
  1534. Revision 1.59 2003/05/12 17:22:00 jonas
  1535. * fixed (last?) remaining -tvarsym(X).address to
  1536. tg.direction*tvarsym(X).address...
  1537. Revision 1.58 2003/04/27 11:21:34 peter
  1538. * aktprocdef renamed to current_procinfo.procdef
  1539. * procinfo renamed to current_procinfo
  1540. * procinfo will now be stored in current_module so it can be
  1541. cleaned up properly
  1542. * gen_main_procsym changed to create_main_proc and release_main_proc
  1543. to also generate a tprocinfo structure
  1544. * fixed unit implicit initfinal
  1545. Revision 1.57 2003/04/27 07:29:51 peter
  1546. * current_procinfo.procdef cleanup, current_procinfo.procdef is now always nil when parsing
  1547. a new procdef declaration
  1548. * aktprocsym removed
  1549. * lexlevel removed, use symtable.symtablelevel instead
  1550. * implicit init/final code uses the normal genentry/genexit
  1551. * funcret state checking updated for new funcret handling
  1552. Revision 1.56 2003/04/25 20:59:34 peter
  1553. * removed funcretn,funcretsym, function result is now in varsym
  1554. and aliases for result and function name are added using absolutesym
  1555. * vs_hidden parameter for funcret passed in parameter
  1556. * vs_hidden fixes
  1557. * writenode changed to printnode and released from extdebug
  1558. * -vp option added to generate a tree.log with the nodetree
  1559. * nicer printnode for statements, callnode
  1560. Revision 1.55 2003/04/06 21:11:23 olle
  1561. * changed newasmsymbol to newasmsymboldata for data symbols
  1562. Revision 1.54 2003/03/28 19:16:57 peter
  1563. * generic constructor working for i386
  1564. * remove fixed self register
  1565. * esi added as address register for i386
  1566. Revision 1.53 2003/02/19 22:00:14 daniel
  1567. * Code generator converted to new register notation
  1568. - Horribily outdated todo.txt removed
  1569. Revision 1.52 2003/01/08 18:43:56 daniel
  1570. * Tregister changed into a record
  1571. Revision 1.51 2002/12/14 15:02:03 carl
  1572. * maxoperands -> max_operands (for portability in rautils.pas)
  1573. * fix some range-check errors with loadconst
  1574. + add ncgadd unit to m68k
  1575. * some bugfix of a_param_reg with LOC_CREFERENCE
  1576. Revision 1.50 2002/11/25 17:43:23 peter
  1577. * splitted defbase in defutil,symutil,defcmp
  1578. * merged isconvertable and is_equal into compare_defs(_ext)
  1579. * made operator search faster by walking the list only once
  1580. Revision 1.49 2002/11/22 22:48:10 carl
  1581. * memory optimization with tconstsym (1.5%)
  1582. Revision 1.48 2002/11/18 17:31:59 peter
  1583. * pass proccalloption to ret_in_xxx and push_xxx functions
  1584. Revision 1.47 2002/11/15 16:29:31 peter
  1585. * made tasmsymbol.refs private (merged)
  1586. Revision 1.46 2002/09/03 16:26:27 daniel
  1587. * Make Tprocdef.defs protected
  1588. Revision 1.45 2002/08/25 19:25:20 peter
  1589. * sym.insert_in_data removed
  1590. * symtable.insertvardata/insertconstdata added
  1591. * removed insert_in_data call from symtable.insert, it needs to be
  1592. called separatly. This allows to deref the address calculation
  1593. * procedures now calculate the parast addresses after the procedure
  1594. directives are parsed. This fixes the cdecl parast problem
  1595. * push_addr_param has an extra argument that specifies if cdecl is used
  1596. or not
  1597. Revision 1.44 2002/08/17 09:23:41 florian
  1598. * first part of procinfo rewrite
  1599. Revision 1.43 2002/08/16 14:24:59 carl
  1600. * issameref() to test if two references are the same (then emit no opcodes)
  1601. + ret_in_reg to replace ret_in_acc
  1602. (fix some register allocation bugs at the same time)
  1603. + save_std_register now has an extra parameter which is the
  1604. usedinproc registers
  1605. Revision 1.42 2002/08/13 18:01:52 carl
  1606. * rename swatoperands to swapoperands
  1607. + m68k first compilable version (still needs a lot of testing):
  1608. assembler generator, system information , inline
  1609. assembler reader.
  1610. Revision 1.41 2002/08/12 15:08:40 carl
  1611. + stab register indexes for powerpc (moved from gdb to cpubase)
  1612. + tprocessor enumeration moved to cpuinfo
  1613. + linker in target_info is now a class
  1614. * many many updates for m68k (will soon start to compile)
  1615. - removed some ifdef or correct them for correct cpu
  1616. Revision 1.40 2002/08/11 14:32:27 peter
  1617. * renamed current_library to objectlibrary
  1618. Revision 1.39 2002/08/11 13:24:13 peter
  1619. * saving of asmsymbols in ppu supported
  1620. * asmsymbollist global is removed and moved into a new class
  1621. tasmlibrarydata that will hold the info of a .a file which
  1622. corresponds with a single module. Added librarydata to tmodule
  1623. to keep the library info stored for the module. In the future the
  1624. objectfiles will also be stored to the tasmlibrarydata class
  1625. * all getlabel/newasmsymbol and friends are moved to the new class
  1626. Revision 1.38 2002/07/20 11:57:57 florian
  1627. * types.pas renamed to defbase.pas because D6 contains a types
  1628. unit so this would conflicts if D6 programms are compiled
  1629. + Willamette/SSE2 instructions to assembler added
  1630. Revision 1.37 2002/07/11 14:41:28 florian
  1631. * start of the new generic parameter handling
  1632. Revision 1.36 2002/07/01 18:46:25 peter
  1633. * internal linker
  1634. * reorganized aasm layer
  1635. Revision 1.35 2002/05/18 13:34:17 peter
  1636. * readded missing revisions
  1637. Revision 1.34 2002/05/16 19:46:44 carl
  1638. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1639. + try to fix temp allocation (still in ifdef)
  1640. + generic constructor calls
  1641. + start of tassembler / tmodulebase class cleanup
  1642. Revision 1.31 2002/05/12 16:53:10 peter
  1643. * moved entry and exitcode to ncgutil and cgobj
  1644. * foreach gets extra argument for passing local data to the
  1645. iterator function
  1646. * -CR checks also class typecasts at runtime by changing them
  1647. into as
  1648. * fixed compiler to cycle with the -CR option
  1649. * fixed stabs with elf writer, finally the global variables can
  1650. be watched
  1651. * removed a lot of routines from cga unit and replaced them by
  1652. calls to cgobj
  1653. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1654. u32bit then the other is typecasted also to u32bit without giving
  1655. a rangecheck warning/error.
  1656. * fixed pascal calling method with reversing also the high tree in
  1657. the parast, detected by tcalcst3 test
  1658. Revision 1.30 2002/04/20 21:32:24 carl
  1659. + generic FPC_CHECKPOINTER
  1660. + first parameter offset in stack now portable
  1661. * rename some constants
  1662. + move some cpu stuff to other units
  1663. - remove unused constents
  1664. * fix stacksize for some targets
  1665. * fix generic size problems which depend now on EXTEND_SIZE constant
  1666. Revision 1.29 2002/04/15 19:02:35 carl
  1667. + target_info.size_of_pointer -> pointer_Size
  1668. Revision 1.28 2002/04/02 17:11:29 peter
  1669. * tlocation,treference update
  1670. * LOC_CONSTANT added for better constant handling
  1671. * secondadd splitted in multiple routines
  1672. * location_force_reg added for loading a location to a register
  1673. of a specified size
  1674. * secondassignment parses now first the right and then the left node
  1675. (this is compatible with Kylix). This saves a lot of push/pop especially
  1676. with string operations
  1677. * adapted some routines to use the new cg methods
  1678. Revision 1.27 2002/01/29 21:32:03 peter
  1679. * allow accessing locals in other lexlevel when the current assembler
  1680. routine doesn't have locals.
  1681. Revision 1.26 2002/01/24 18:25:50 peter
  1682. * implicit result variable generation for assembler routines
  1683. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1684. }