rautils.pas 47 KB

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