rautils.pas 47 KB

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