rautils.pas 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 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. Interface
  20. Uses
  21. cutils,cobjects,
  22. globtype,aasm,cpubase,symconst;
  23. Const
  24. RPNMax = 10; { I think you only need 4, but just to be safe }
  25. OpMax = 25;
  26. maxoperands = 3; { Maximum operands for assembler instructions }
  27. {---------------------------------------------------------------------
  28. Local Label Management
  29. ---------------------------------------------------------------------}
  30. Type
  31. { Each local label has this structure associated with it }
  32. PLocalLabel = ^TLocalLabel;
  33. TLocalLabel = object(TNamedIndexObject)
  34. Emitted : boolean;
  35. constructor Init(const n:string);
  36. function Getpasmlabel:pasmlabel;
  37. private
  38. lab : pasmlabel;
  39. end;
  40. PLocalLabelList = ^TLocalLabelList;
  41. TLocalLabelList = Object(TDictionary)
  42. procedure CheckEmitted;
  43. end;
  44. var
  45. LocalLabelList : PLocalLabelList;
  46. function CreateLocalLabel(const s: string; var hl: pasmlabel; emit:boolean):boolean;
  47. Function SearchLabel(const s: string; var hl: pasmlabel;emit:boolean): boolean;
  48. {---------------------------------------------------------------------
  49. Instruction management
  50. ---------------------------------------------------------------------}
  51. type
  52. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_REFERENCE,OPR_REGISTER);
  53. TOprRec = record
  54. case typ:TOprType of
  55. OPR_NONE : ();
  56. OPR_CONSTANT : (val:longint);
  57. OPR_SYMBOL : (symbol:PAsmSymbol;symofs:longint);
  58. OPR_REFERENCE : (ref:treference);
  59. OPR_REGISTER : (reg:tregister);
  60. end;
  61. POperand = ^TOperand;
  62. TOperand = object
  63. size : topsize;
  64. hastype, { if the operand has typecasted variable }
  65. hasvar : boolean; { if the operand is loaded with a variable }
  66. opr : TOprRec;
  67. constructor init;
  68. destructor done;virtual;
  69. Procedure BuildOperand;virtual;
  70. Procedure SetSize(_size:longint;force:boolean);
  71. Procedure SetCorrectSize(opcode:tasmop);virtual;
  72. Function SetupResult:boolean;
  73. Function SetupSelf:boolean;
  74. Function SetupOldEBP:boolean;
  75. Function SetupVar(const hs:string;GetOffset : boolean): Boolean;
  76. Function SetupDirectVar(const hs:string): Boolean;
  77. Procedure InitRef;
  78. end;
  79. PInstruction = ^TInstruction;
  80. TInstruction = object
  81. opcode : tasmop;
  82. opsize : topsize;
  83. condition : tasmcond;
  84. ops : byte;
  85. operands : array[1..maxoperands] of POperand;
  86. constructor init;
  87. destructor done;virtual;
  88. Procedure InitOperands;virtual;
  89. Procedure BuildOpcode;virtual;
  90. procedure ConcatInstruction(p:PAasmoutput);virtual;
  91. Procedure SwapOperands;
  92. end;
  93. {---------------------------------------------------------------------}
  94. { Expression parser types }
  95. {---------------------------------------------------------------------}
  96. TExprOperator = record
  97. ch: char; { operator }
  98. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  99. end;
  100. String15 = String[15];
  101. {**********************************************************************}
  102. { The following operators are supported: }
  103. { '+' : addition }
  104. { '-' : subtraction }
  105. { '*' : multiplication }
  106. { '/' : modulo division }
  107. { '^' : exclusive or }
  108. { '<' : shift left }
  109. { '>' : shift right }
  110. { '&' : bitwise and }
  111. { '|' : bitwise or }
  112. { '~' : bitwise complement }
  113. { '%' : modulo division }
  114. { nnn: longint numbers }
  115. { ( and ) parenthesis }
  116. {**********************************************************************}
  117. TExprParse = Object
  118. public
  119. Constructor Init;
  120. Destructor Done;
  121. Function Evaluate(Expr: String): longint;
  122. Function Priority(_Operator: Char): Integer; virtual;
  123. private
  124. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  125. RPNTop : Integer;
  126. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  127. OpTop : Integer;
  128. Procedure RPNPush(Num: Longint);
  129. Function RPNPop: Longint;
  130. Procedure RPNCalc(token: String15; prefix: boolean);
  131. Procedure OpPush(_Operator: char; prefix: boolean);
  132. { In reality returns TExprOperaotr }
  133. Procedure OpPop(var _Operator:TExprOperator);
  134. end;
  135. { Evaluate an expression string to a longint }
  136. Function CalculateExpression(const expression: string): longint;
  137. {---------------------------------------------------------------------}
  138. { String routines }
  139. {---------------------------------------------------------------------}
  140. Function ValDecimal(const S:String):longint;
  141. Function ValOctal(const S:String):longint;
  142. Function ValBinary(const S:String):longint;
  143. Function ValHexaDecimal(const S:String):longint;
  144. Function PadZero(Var s: String; n: byte): Boolean;
  145. Function EscapeToPascal(const s:string): string;
  146. {---------------------------------------------------------------------
  147. Symbol helper routines
  148. ---------------------------------------------------------------------}
  149. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  150. Function SearchType(const hs:string): Boolean;
  151. Function SearchRecordType(const s:string): boolean;
  152. Function SearchIConstant(const s:string; var l:longint): boolean;
  153. {---------------------------------------------------------------------
  154. Instruction generation routines
  155. ---------------------------------------------------------------------}
  156. Procedure ConcatPasString(p : paasmoutput;s:string);
  157. Procedure ConcatDirect(p : paasmoutput;s:string);
  158. Procedure ConcatLabel(p: paasmoutput;var l : pasmlabel);
  159. Procedure ConcatConstant(p : paasmoutput;value: longint; maxvalue: longint);
  160. Procedure ConcatConstSymbol(p : paasmoutput;const sym:string;l:longint);
  161. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  162. Procedure ConcatString(p : paasmoutput;s:string);
  163. procedure ConcatAlign(p:paasmoutput;l:longint);
  164. Procedure ConcatPublic(p:paasmoutput;const s : string);
  165. Procedure ConcatLocal(p:paasmoutput;const s : string);
  166. Procedure ConcatGlobalBss(const s : string;size : longint);
  167. Procedure ConcatLocalBss(const s : string;size : longint);
  168. Implementation
  169. uses
  170. strings,
  171. types,systems,verbose,globals,fmodule,
  172. symtable,cpuasm
  173. {$ifdef NEWCG}
  174. ,cgbase;
  175. {$else}
  176. ,hcodegen;
  177. {$endif}
  178. {*************************************************************************
  179. TExprParse
  180. *************************************************************************}
  181. Constructor TExprParse.Init;
  182. Begin
  183. end;
  184. Procedure TExprParse.RPNPush(Num : longint);
  185. { Add an operand to the top of the RPN stack }
  186. begin
  187. if RPNTop < RPNMax then
  188. begin
  189. Inc(RPNTop);
  190. RPNStack[RPNTop]:=Num;
  191. end
  192. else
  193. Message(asmr_e_expr_illegal);
  194. end;
  195. Function TExprParse.RPNPop : longint;
  196. { Get the operand at the top of the RPN stack }
  197. begin
  198. if RPNTop > 0 then
  199. begin
  200. RPNPop:=RPNStack[RPNTop];
  201. Dec(RPNTop);
  202. end
  203. else
  204. Message(asmr_e_expr_illegal);
  205. end;
  206. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  207. Var
  208. Temp : longint;
  209. n1,n2 : longint;
  210. LocalError : Integer;
  211. begin
  212. { Handle operators }
  213. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  214. Case Token[1] of
  215. '+' :
  216. Begin
  217. if not prefix then
  218. RPNPush(RPNPop + RPNPop);
  219. end;
  220. '-' :
  221. Begin
  222. if prefix then
  223. RPNPush(-(RPNPop))
  224. else
  225. begin
  226. n1:=RPNPop;
  227. n2:=RPNPop;
  228. RPNPush(n2 - n1);
  229. end;
  230. end;
  231. '*' : RPNPush(RPNPop * RPNPop);
  232. '&' :
  233. begin
  234. n1:=RPNPop;
  235. n2:=RPNPop;
  236. RPNPush(n2 and n1);
  237. end;
  238. '|' :
  239. begin
  240. n1:=RPNPop;
  241. n2:=RPNPop;
  242. RPNPush(n2 or n1);
  243. end;
  244. '~' : RPNPush(NOT RPNPop);
  245. '<' :
  246. begin
  247. n1:=RPNPop;
  248. n2:=RPNPop;
  249. RPNPush(n2 SHL n1);
  250. end;
  251. '>' :
  252. begin
  253. n1:=RPNPop;
  254. n2:=RPNPop;
  255. RPNPush(n2 SHR n1);
  256. end;
  257. '%' :
  258. begin
  259. Temp:=RPNPop;
  260. if Temp <> 0 then
  261. RPNPush(RPNPop mod Temp)
  262. else
  263. begin
  264. Message(asmr_e_expr_zero_divide);
  265. { push 1 for error recovery }
  266. RPNPush(1);
  267. end;
  268. end;
  269. '^' : RPNPush(RPNPop XOR RPNPop);
  270. '/' :
  271. begin
  272. Temp:=RPNPop;
  273. if Temp <> 0 then
  274. RPNPush(RPNPop div Temp)
  275. else
  276. begin
  277. Message(asmr_e_expr_zero_divide);
  278. { push 1 for error recovery }
  279. RPNPush(1);
  280. end;
  281. end;
  282. end
  283. else
  284. begin
  285. { Convert String to number and add to stack }
  286. if token='-2147483648' then
  287. begin
  288. temp:=$80000000;
  289. localerror:=0;
  290. end
  291. else
  292. Val(Token, Temp, LocalError);
  293. if LocalError = 0 then
  294. RPNPush(Temp)
  295. else
  296. begin
  297. Message(asmr_e_expr_illegal);
  298. { push 1 for error recovery }
  299. RPNPush(1);
  300. end;
  301. end;
  302. end;
  303. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  304. { Add an operator onto top of the stack }
  305. begin
  306. if OpTop < OpMax then
  307. begin
  308. Inc(OpTop);
  309. OpStack[OpTop].ch:=_Operator;
  310. OpStack[OpTop].is_prefix:=prefix;
  311. end
  312. else
  313. Message(asmr_e_expr_illegal);
  314. end;
  315. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  316. { Get operator at the top of the stack }
  317. begin
  318. if OpTop > 0 then
  319. begin
  320. _Operator:=OpStack[OpTop];
  321. Dec(OpTop);
  322. end
  323. else
  324. Message(asmr_e_expr_illegal);
  325. end;
  326. Function TExprParse.Priority(_Operator : Char) : Integer;
  327. { Return priority of operator }
  328. { The greater the priority, the higher the precedence }
  329. begin
  330. Case _Operator OF
  331. '(' :
  332. Priority:=0;
  333. '+', '-' :
  334. Priority:=1;
  335. '*', '/','%','<','>' :
  336. Priority:=2;
  337. '|','&','^','~' :
  338. Priority:=0;
  339. else
  340. Message(asmr_e_expr_illegal);
  341. end;
  342. end;
  343. Function TExprParse.Evaluate(Expr : String):longint;
  344. Var
  345. I : Integer;
  346. Token : String15;
  347. opr : TExprOperator;
  348. begin
  349. Evaluate:=0;
  350. { Reset stacks }
  351. OpTop :=0;
  352. RPNTop:=0;
  353. Token :='';
  354. { nothing to do ? }
  355. if Expr='' then
  356. exit;
  357. For I:=1 to Length(Expr) DO
  358. begin
  359. if Expr[I] in ['0'..'9'] then
  360. begin { Build multi-digit numbers }
  361. Token:=Token + Expr[I];
  362. if I = Length(Expr) then { Send last one to calculator }
  363. RPNCalc(Token,false);
  364. end
  365. else
  366. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  367. begin
  368. if Token <> '' then
  369. begin { Send last built number to calc. }
  370. RPNCalc(Token,false);
  371. Token:='';
  372. end;
  373. Case Expr[I] OF
  374. '(' : OpPush('(',false);
  375. ')' : begin
  376. While OpStack[OpTop].ch <> '(' DO
  377. Begin
  378. OpPop(opr);
  379. RPNCalc(opr.ch,opr.is_prefix);
  380. end;
  381. OpPop(opr); { Pop off and ignore the '(' }
  382. end;
  383. '+','-','~' : Begin
  384. { workaround for -2147483648 }
  385. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  386. begin
  387. token:='-';
  388. expr[i]:='+';
  389. end;
  390. { if start of expression then surely a prefix }
  391. { or if previous char was also an operator }
  392. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  393. OpPush(Expr[I],true)
  394. else
  395. Begin
  396. { Evaluate all higher priority operators }
  397. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  398. Begin
  399. OpPop(opr);
  400. RPNCalc(opr.ch,opr.is_prefix);
  401. end;
  402. OpPush(Expr[I],false);
  403. End;
  404. end;
  405. '*', '/',
  406. '^','|','&',
  407. '%','<','>' : begin
  408. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  409. Begin
  410. OpPop(opr);
  411. RPNCalc(opr.ch,opr.is_prefix);
  412. end;
  413. OpPush(Expr[I],false);
  414. end;
  415. end; { Case }
  416. end
  417. else
  418. Message(asmr_e_expr_illegal); { Handle bad input error }
  419. end;
  420. { Pop off the remaining operators }
  421. While OpTop > 0 do
  422. Begin
  423. OpPop(opr);
  424. RPNCalc(opr.ch,opr.is_prefix);
  425. end;
  426. { The result is stored on the top of the stack }
  427. Evaluate:=RPNPop;
  428. end;
  429. Destructor TExprParse.Done;
  430. Begin
  431. end;
  432. Function CalculateExpression(const expression: string): longint;
  433. var
  434. expr: TExprParse;
  435. Begin
  436. expr.Init;
  437. CalculateExpression:=expr.Evaluate(expression);
  438. expr.Done;
  439. end;
  440. {*************************************************************************}
  441. { String conversions/utils }
  442. {*************************************************************************}
  443. Function EscapeToPascal(const s:string): string;
  444. { converts a C styled string - which contains escape }
  445. { characters to a pascal style string. }
  446. var
  447. i,len : longint;
  448. hs : string;
  449. temp : string;
  450. c : char;
  451. Begin
  452. hs:='';
  453. len:=0;
  454. i:=0;
  455. while (i<length(s)) and (len<255) do
  456. begin
  457. Inc(i);
  458. if (s[i]='\') and (i<length(s)) then
  459. Begin
  460. inc(i);
  461. case s[i] of
  462. '\' :
  463. c:='\';
  464. 'b':
  465. c:=#8;
  466. 'f':
  467. c:=#12;
  468. 'n':
  469. c:=#10;
  470. 'r':
  471. c:=#13;
  472. 't':
  473. c:=#9;
  474. '"':
  475. c:='"';
  476. '0'..'7':
  477. Begin
  478. temp:=s[i];
  479. temp:=temp+s[i+1];
  480. temp:=temp+s[i+2];
  481. inc(i,2);
  482. c:=chr(ValOctal(temp));
  483. end;
  484. 'x':
  485. Begin
  486. temp:=s[i+1];
  487. temp:=temp+s[i+2];
  488. inc(i,2);
  489. c:=chr(ValHexaDecimal(temp));
  490. end;
  491. else
  492. Begin
  493. Message1(asmr_e_escape_seq_ignored,s[i]);
  494. c:=s[i];
  495. end;
  496. end;
  497. end
  498. else
  499. c:=s[i];
  500. inc(len);
  501. hs[len]:=c;
  502. end;
  503. hs[0]:=chr(len);
  504. EscapeToPascal:=hs;
  505. end;
  506. Function ValDecimal(const S:String):longint;
  507. { Converts a decimal string to longint }
  508. var
  509. vs,c : longint;
  510. Begin
  511. vs:=0;
  512. for c:=1 to length(s) do
  513. begin
  514. vs:=vs*10;
  515. if s[c] in ['0'..'9'] then
  516. inc(vs,ord(s[c])-ord('0'))
  517. else
  518. begin
  519. Message1(asmr_e_error_converting_decimal,s);
  520. ValDecimal:=0;
  521. exit;
  522. end;
  523. end;
  524. ValDecimal:=vs;
  525. end;
  526. Function ValOctal(const S:String):longint;
  527. { Converts an octal string to longint }
  528. var
  529. vs,c : longint;
  530. Begin
  531. vs:=0;
  532. for c:=1 to length(s) do
  533. begin
  534. vs:=vs shl 3;
  535. if s[c] in ['0'..'7'] then
  536. inc(vs,ord(s[c])-ord('0'))
  537. else
  538. begin
  539. Message1(asmr_e_error_converting_octal,s);
  540. ValOctal:=0;
  541. exit;
  542. end;
  543. end;
  544. ValOctal:=vs;
  545. end;
  546. Function ValBinary(const S:String):longint;
  547. { Converts a binary string to longint }
  548. var
  549. vs,c : longint;
  550. Begin
  551. vs:=0;
  552. for c:=1 to length(s) do
  553. begin
  554. vs:=vs shl 1;
  555. if s[c] in ['0'..'1'] then
  556. inc(vs,ord(s[c])-ord('0'))
  557. else
  558. begin
  559. Message1(asmr_e_error_converting_binary,s);
  560. ValBinary:=0;
  561. exit;
  562. end;
  563. end;
  564. ValBinary:=vs;
  565. end;
  566. Function ValHexadecimal(const S:String):longint;
  567. { Converts a binary string to longint }
  568. var
  569. vs,c : longint;
  570. Begin
  571. vs:=0;
  572. for c:=1 to length(s) do
  573. begin
  574. vs:=vs shl 4;
  575. case s[c] of
  576. '0'..'9' :
  577. inc(vs,ord(s[c])-ord('0'));
  578. 'A'..'F' :
  579. inc(vs,ord(s[c])-ord('A')+10);
  580. 'a'..'f' :
  581. inc(vs,ord(s[c])-ord('a')+10);
  582. else
  583. begin
  584. Message1(asmr_e_error_converting_hexadecimal,s);
  585. ValHexadecimal:=0;
  586. exit;
  587. end;
  588. end;
  589. end;
  590. ValHexadecimal:=vs;
  591. end;
  592. Function PadZero(Var s: String; n: byte): Boolean;
  593. Begin
  594. PadZero:=TRUE;
  595. { Do some error checking first }
  596. if Length(s) = n then
  597. exit
  598. else
  599. if Length(s) > n then
  600. Begin
  601. PadZero:=FALSE;
  602. delete(s,n+1,length(s));
  603. exit;
  604. end
  605. else
  606. PadZero:=TRUE;
  607. { Fill it up with the specified character }
  608. fillchar(s[length(s)+1],n-1,#0);
  609. s[0]:=chr(n);
  610. end;
  611. {****************************************************************************
  612. TOperand
  613. ****************************************************************************}
  614. constructor TOperand.init;
  615. begin
  616. size:=S_NO;
  617. hastype:=false;
  618. hasvar:=false;
  619. FillChar(Opr,sizeof(Opr),0);
  620. end;
  621. destructor TOperand.done;
  622. begin
  623. end;
  624. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  625. begin
  626. end;
  627. Procedure TOperand.SetSize(_size:longint;force:boolean);
  628. begin
  629. if force or
  630. ((size = S_NO) and (_size<=extended_size)) then
  631. Begin
  632. case _size of
  633. 1 : size:=S_B;
  634. 2 : size:=S_W{ could be S_IS};
  635. 4 : size:=S_L{ could be S_IL or S_FS};
  636. 8 : size:=S_IQ{ could be S_D or S_FL};
  637. extended_size : size:=S_FX;
  638. end;
  639. end;
  640. end;
  641. Function TOperand.SetupResult:boolean;
  642. Begin
  643. SetupResult:=false;
  644. { replace by correct offset. }
  645. if assigned(procinfo^.returntype.def) and
  646. (procinfo^.returntype.def<>pdef(voiddef)) then
  647. begin
  648. opr.ref.offset:=procinfo^.return_offset;
  649. opr.ref.base:= procinfo^.framepointer;
  650. { always assume that the result is valid. }
  651. procinfo^.funcret_state:=vs_assigned;
  652. SetupResult:=true;
  653. end
  654. else
  655. Message(asmr_e_void_function);
  656. end;
  657. Function TOperand.SetupSelf:boolean;
  658. Begin
  659. SetupSelf:=false;
  660. if assigned(procinfo^._class) then
  661. Begin
  662. opr.typ:=OPR_REFERENCE;
  663. opr.ref.offset:=procinfo^.selfpointer_offset;
  664. opr.ref.base:=procinfo^.framepointer;
  665. opr.ref.options:=ref_selffixup;
  666. SetupSelf:=true;
  667. end
  668. else
  669. Message(asmr_e_cannot_use_SELF_outside_a_method);
  670. end;
  671. Function TOperand.SetupOldEBP:boolean;
  672. Begin
  673. SetupOldEBP:=false;
  674. if lexlevel>normal_function_level then
  675. Begin
  676. opr.typ:=OPR_REFERENCE;
  677. opr.ref.offset:=procinfo^.framepointer_offset;
  678. opr.ref.base:=procinfo^.framepointer;
  679. SetupOldEBP:=true;
  680. end
  681. else
  682. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  683. end;
  684. Function TOperand.SetupVar(const hs:string;GetOffset : boolean): Boolean;
  685. { search and sets up the correct fields in the Instr record }
  686. { for the NON-constant identifier passed to the routine. }
  687. { if not found returns FALSE. }
  688. var
  689. sym : psym;
  690. harrdef : parraydef;
  691. Begin
  692. SetupVar:=false;
  693. { are we in a routine ? }
  694. getsym(hs,false);
  695. sym:=srsym;
  696. if sym=nil then
  697. exit;
  698. case sym^.typ of
  699. varsym :
  700. begin
  701. { we always assume in asm statements that }
  702. { that the variable is valid. }
  703. pvarsym(sym)^.varstate:=vs_used;
  704. inc(pvarsym(sym)^.refs);
  705. case pvarsym(sym)^.owner^.symtabletype of
  706. objectsymtable :
  707. begin
  708. { this is not allowed, because we don't know if the self
  709. register is still free, and loading it first is also
  710. not possible, because this could break code }
  711. { Be TP/Delphi compatible in Delphi or TP modes }
  712. if (m_tp in aktmodeswitches) then
  713. begin
  714. opr.typ:=OPR_CONSTANT;
  715. opr.val:=pvarsym(sym)^.address;
  716. end
  717. { I do not agree here people using method vars should ensure
  718. that %esi is valid there }
  719. else
  720. begin
  721. opr.ref.base:=self_pointer;
  722. opr.ref.offset:=pvarsym(sym)^.address;
  723. end;
  724. hasvar:=true;
  725. SetupVar:=true;
  726. Exit;
  727. end;
  728. unitsymtable,
  729. globalsymtable,
  730. staticsymtable :
  731. opr.ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname);
  732. parasymtable :
  733. begin
  734. { if we only want the offset we don't have to care
  735. the base will be zeroed after ! }
  736. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) or
  737. { this below is wrong because there are two parast
  738. for global functions one of interface the second of
  739. implementation
  740. if (pvarsym(sym)^.owner=procinfo^.def^.parast) or }
  741. GetOffset then
  742. begin
  743. opr.ref.base:=procinfo^.framepointer;
  744. end
  745. else
  746. begin
  747. if (procinfo^.framepointer=R_ESP) and
  748. assigned(procinfo^.parent) and
  749. (lexlevel=pvarsym(sym)^.owner^.symtablelevel+1) and
  750. { same problem as above !!
  751. (procinfo^.parent^.sym^.definition^.parast=pvarsym(sym)^.owner) and }
  752. (lexlevel>normal_function_level) then
  753. opr.ref.base:=procinfo^.parent^.framepointer
  754. else
  755. message1(asmr_e_local_para_unreachable,hs);
  756. end;
  757. opr.ref.offset:=pvarsym(sym)^.address;
  758. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) then
  759. begin
  760. opr.ref.offsetfixup:=aktprocsym^.definition^.parast^.address_fixup;
  761. opr.ref.options:=ref_parafixup;
  762. end
  763. else
  764. begin
  765. opr.ref.offsetfixup:=0;
  766. opr.ref.options:=ref_none;
  767. end;
  768. if (pvarsym(sym)^.varspez=vs_var) or
  769. ((pvarsym(sym)^.varspez=vs_const) and
  770. push_addr_param(pvarsym(sym)^.vartype.def)) then
  771. SetSize(target_os.size_of_pointer,false);
  772. end;
  773. localsymtable :
  774. begin
  775. if (vo_is_external in pvarsym(sym)^.varoptions) then
  776. opr.ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname)
  777. else
  778. begin
  779. { if we only want the offset we don't have to care
  780. the base will be zeroed after ! }
  781. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) or
  782. {if (pvarsym(sym)^.owner=procinfo^.def^.localst) or}
  783. GetOffset then
  784. opr.ref.base:=procinfo^.framepointer
  785. else
  786. begin
  787. if (procinfo^.framepointer=R_ESP) and
  788. assigned(procinfo^.parent) and
  789. (lexlevel=pvarsym(sym)^.owner^.symtablelevel+1) and
  790. {(procinfo^.parent^.sym^.definition^.localst=pvarsym(sym)^.owner) and}
  791. (lexlevel>normal_function_level) then
  792. opr.ref.base:=procinfo^.parent^.framepointer
  793. else
  794. message1(asmr_e_local_para_unreachable,hs);
  795. end;
  796. opr.ref.offset:=-(pvarsym(sym)^.address);
  797. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) then
  798. begin
  799. opr.ref.offsetfixup:=aktprocsym^.definition^.localst^.address_fixup;
  800. opr.ref.options:=ref_localfixup;
  801. end
  802. else
  803. begin
  804. opr.ref.offsetfixup:=0;
  805. opr.ref.options:=ref_none;
  806. end;
  807. end;
  808. end;
  809. end;
  810. case pvarsym(sym)^.vartype.def^.deftype of
  811. orddef,
  812. enumdef,
  813. pointerdef,
  814. floatdef :
  815. SetSize(pvarsym(sym)^.getsize,false);
  816. arraydef :
  817. begin
  818. { for arrays try to get the element size, take care of
  819. multiple indexes }
  820. harrdef:=Parraydef(PVarsym(sym)^.vartype.def);
  821. while assigned(harrdef^.elementtype.def) and
  822. (harrdef^.elementtype.def^.deftype=arraydef) do
  823. harrdef:=parraydef(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. opr.ref.symbol:=newasmsymbol(ptypedconstsym(sym)^.mangledname);
  834. case ptypedconstsym(sym)^.typedconsttype.def^.deftype of
  835. orddef,
  836. enumdef,
  837. pointerdef,
  838. floatdef :
  839. SetSize(ptypedconstsym(sym)^.getsize,false);
  840. arraydef :
  841. begin
  842. { for arrays try to get the element size, take care of
  843. multiple indexes }
  844. harrdef:=Parraydef(PTypedConstSym(sym)^.typedconsttype.def);
  845. while assigned(harrdef^.elementtype.def) and
  846. (harrdef^.elementtype.def^.deftype=arraydef) do
  847. harrdef:=parraydef(harrdef^.elementtype.def);
  848. SetSize(harrdef^.elesize,false);
  849. end;
  850. end;
  851. hasvar:=true;
  852. SetupVar:=true;
  853. Exit;
  854. end;
  855. constsym :
  856. begin
  857. if pconstsym(sym)^.consttyp in [constint,constchar,constbool] then
  858. begin
  859. opr.typ:=OPR_CONSTANT;
  860. opr.val:=pconstsym(sym)^.value;
  861. SetupVar:=true;
  862. Exit;
  863. end;
  864. end;
  865. typesym :
  866. begin
  867. if ptypesym(sym)^.restype.def^.deftype in [recorddef,objectdef] then
  868. begin
  869. opr.typ:=OPR_CONSTANT;
  870. opr.val:=0;
  871. SetupVar:=TRUE;
  872. Exit;
  873. end;
  874. end;
  875. procsym :
  876. begin
  877. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  878. Message(asmr_w_calling_overload_func);
  879. opr.typ:=OPR_SYMBOL;
  880. opr.symbol:=newasmsymbol(pprocsym(sym)^.definition^.mangledname);
  881. hasvar:=true;
  882. SetupVar:=TRUE;
  883. Exit;
  884. end;
  885. else
  886. begin
  887. Message(asmr_e_unsupported_symbol_type);
  888. exit;
  889. end;
  890. end;
  891. end;
  892. { looks for internal names of variables and routines }
  893. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  894. {$ifndef OLDDIRECTVAR}
  895. var
  896. p : pasmsymbol;
  897. begin
  898. SetupDirectVar:=false;
  899. p:=getasmsymbol(hs);
  900. if assigned(p) then
  901. begin
  902. opr.ref.symbol:=p;
  903. hasvar:=true;
  904. SetupDirectVar:=true;
  905. end;
  906. end;
  907. {$else}
  908. var
  909. p : pai_external;
  910. Begin
  911. SearchDirectVar:=false;
  912. { search in the list of internals }
  913. p:=search_assembler_symbol(internals,hs,EXT_ANY);
  914. if p=nil then
  915. p:=search_assembler_symbol(externals,hs,EXT_ANY);
  916. if p<>nil then
  917. begin
  918. instr.operands[operandnum].opr.ref.symbol:=p^.sym;
  919. case p^.exttyp of
  920. EXT_BYTE : instr.operands[operandnum].size:=S_B;
  921. EXT_WORD : instr.operands[operandnum].size:=S_W;
  922. EXT_NEAR,EXT_FAR,EXT_PROC,EXT_DWORD,EXT_CODEPTR,EXT_DATAPTR:
  923. instr.operands[operandnum].size:=S_L;
  924. EXT_QWORD : instr.operands[operandnum].size:=S_FL;
  925. EXT_TBYTE : instr.operands[operandnum].size:=S_FX;
  926. else
  927. { this is in the case where the instruction is LEA }
  928. { or something like that, in that case size is not }
  929. { important. }
  930. instr.operands[operandnum].size:=S_NO;
  931. end;
  932. instr.operands[operandnum].hasvar:=true;
  933. SearchDirectVar:=TRUE;
  934. Exit;
  935. end;
  936. end;
  937. {$endif}
  938. procedure TOperand.InitRef;
  939. {*********************************************************************}
  940. { Description: This routine first check if the opcode is of }
  941. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  942. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  943. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  944. { to point to the default segment. }
  945. {*********************************************************************}
  946. Begin
  947. case opr.typ of
  948. OPR_REFERENCE:
  949. exit;
  950. OPR_NONE: ;
  951. else
  952. Message(asmr_e_invalid_operand_type);
  953. end;
  954. opr.typ := OPR_REFERENCE;
  955. reset_reference(opr.ref);
  956. end;
  957. procedure TOperand.BuildOperand;
  958. begin
  959. abstract;
  960. end;
  961. {****************************************************************************
  962. TInstruction
  963. ****************************************************************************}
  964. constructor TInstruction.init;
  965. Begin
  966. Opcode:=A_NONE;
  967. Opsize:=S_NO;
  968. Condition:=C_NONE;
  969. Ops:=0;
  970. InitOperands;
  971. end;
  972. destructor TInstruction.done;
  973. var
  974. i : longint;
  975. Begin
  976. for i:=1 to 3 do
  977. Dispose(Operands[i],Done);
  978. end;
  979. procedure TInstruction.InitOperands;
  980. var
  981. i : longint;
  982. begin
  983. for i:=1 to 3 do
  984. New(Operands[i],init);
  985. end;
  986. Procedure TInstruction.SwapOperands;
  987. Var
  988. p : POperand;
  989. Begin
  990. case Ops of
  991. 2 :
  992. begin
  993. p:=Operands[1];
  994. Operands[1]:=Operands[2];
  995. Operands[2]:=p;
  996. end;
  997. 3 :
  998. begin
  999. p:=Operands[1];
  1000. Operands[1]:=Operands[3];
  1001. Operands[3]:=p;
  1002. end;
  1003. end;
  1004. end;
  1005. procedure TInstruction.ConcatInstruction(p:PAasmOutput);
  1006. begin
  1007. abstract;
  1008. end;
  1009. procedure TInstruction.BuildOpcode;
  1010. begin
  1011. abstract;
  1012. end;
  1013. {***************************************************************************
  1014. TLocalLabel
  1015. ***************************************************************************}
  1016. constructor TLocalLabel.Init(const n:string);
  1017. begin
  1018. inherited InitName(n);
  1019. lab:=nil;
  1020. emitted:=false;
  1021. end;
  1022. function TLocalLabel.Getpasmlabel:pasmlabel;
  1023. begin
  1024. if not assigned(lab) then
  1025. begin
  1026. getlabel(lab);
  1027. { this label is forced to be used so it's always written }
  1028. inc(lab^.refs);
  1029. end;
  1030. Getpasmlabel:=lab;
  1031. end;
  1032. {***************************************************************************
  1033. TLocalLabelList
  1034. ***************************************************************************}
  1035. procedure LocalLabelEmitted(p:PNamedIndexObject);{$ifndef FPC}far;{$endif}
  1036. begin
  1037. if not PLocalLabel(p)^.emitted then
  1038. Message1(asmr_e_unknown_label_identifier,p^.name);
  1039. end;
  1040. procedure TLocalLabelList.CheckEmitted;
  1041. begin
  1042. ForEach({$ifndef TP}@{$endif}LocalLabelEmitted)
  1043. end;
  1044. function CreateLocalLabel(const s: string; var hl: pasmlabel; emit:boolean):boolean;
  1045. var
  1046. lab : PLocalLabel;
  1047. Begin
  1048. CreateLocalLabel:=true;
  1049. { Check if it already is defined }
  1050. lab:=PLocalLabel(LocalLabelList^.Search(s));
  1051. if not assigned(lab) then
  1052. begin
  1053. new(lab,init(s));
  1054. LocalLabelList^.Insert(lab);
  1055. end;
  1056. { set emitted flag and check for dup syms }
  1057. if emit then
  1058. begin
  1059. if lab^.Emitted then
  1060. begin
  1061. Message1(asmr_e_dup_local_sym,lab^.Name);
  1062. CreateLocalLabel:=false;
  1063. end;
  1064. lab^.Emitted:=true;
  1065. end;
  1066. hl:=lab^.Getpasmlabel;
  1067. end;
  1068. {****************************************************************************
  1069. Symbol table helper routines
  1070. ****************************************************************************}
  1071. Function SearchType(const hs:string): Boolean;
  1072. begin
  1073. getsym(hs,false);
  1074. SearchType:=assigned(srsym) and
  1075. (srsym^.typ=typesym);
  1076. end;
  1077. Function SearchRecordType(const s:string): boolean;
  1078. Begin
  1079. SearchRecordType:=false;
  1080. { Check the constants in symtable }
  1081. getsym(s,false);
  1082. if srsym <> nil then
  1083. Begin
  1084. case srsym^.typ of
  1085. typesym :
  1086. begin
  1087. if ptypesym(srsym)^.restype.def^.deftype in [recorddef,objectdef] then
  1088. begin
  1089. SearchRecordType:=true;
  1090. exit;
  1091. end;
  1092. end;
  1093. end;
  1094. end;
  1095. end;
  1096. Function SearchIConstant(const s:string; var l:longint): boolean;
  1097. {**********************************************************************}
  1098. { Description: Searches for a CONSTANT of name s in either the local }
  1099. { symbol list, then in the global symbol list, and returns the value }
  1100. { of that constant in l. Returns TRUE if successfull, if not found, }
  1101. { or if the constant is not of correct type, then returns FALSE }
  1102. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1103. { respectively. }
  1104. {**********************************************************************}
  1105. Begin
  1106. SearchIConstant:=false;
  1107. { check for TRUE or FALSE reserved words first }
  1108. if s = 'TRUE' then
  1109. Begin
  1110. SearchIConstant:=TRUE;
  1111. l:=1;
  1112. exit;
  1113. end;
  1114. if s = 'FALSE' then
  1115. Begin
  1116. SearchIConstant:=TRUE;
  1117. l:=0;
  1118. exit;
  1119. end;
  1120. { Check the constants in symtable }
  1121. getsym(s,false);
  1122. if srsym <> nil then
  1123. Begin
  1124. case srsym^.typ of
  1125. constsym :
  1126. begin
  1127. if (pconstsym(srsym)^.consttyp in [constord,constint,constchar,constbool]) then
  1128. Begin
  1129. l:=pconstsym(srsym)^.value;
  1130. SearchIConstant:=TRUE;
  1131. exit;
  1132. end;
  1133. end;
  1134. enumsym:
  1135. Begin
  1136. l:=penumsym(srsym)^.value;
  1137. SearchIConstant:=TRUE;
  1138. exit;
  1139. end;
  1140. end;
  1141. end;
  1142. end;
  1143. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1144. { search and returns the offset and size of records/objects of the base }
  1145. { with field name setup in field. }
  1146. { returns FALSE if not found. }
  1147. { used when base is a variable or a typed constant name. }
  1148. var
  1149. st : psymtable;
  1150. harrdef : parraydef;
  1151. sym : psym;
  1152. i : longint;
  1153. base : string;
  1154. Begin
  1155. GetRecordOffsetSize:=FALSE;
  1156. Offset:=0;
  1157. Size:=0;
  1158. i:=pos('.',s);
  1159. if i=0 then
  1160. i:=255;
  1161. base:=Copy(s,1,i-1);
  1162. delete(s,1,i);
  1163. if base='SELF' then
  1164. st:=procinfo^._class^.symtable
  1165. else
  1166. begin
  1167. getsym(base,false);
  1168. sym:=srsym;
  1169. st:=nil;
  1170. { we can start with a var,type,typedconst }
  1171. case sym^.typ of
  1172. varsym :
  1173. begin
  1174. case pvarsym(sym)^.vartype.def^.deftype of
  1175. recorddef :
  1176. st:=precorddef(pvarsym(sym)^.vartype.def)^.symtable;
  1177. objectdef :
  1178. st:=pobjectdef(pvarsym(sym)^.vartype.def)^.symtable;
  1179. end;
  1180. end;
  1181. typesym :
  1182. begin
  1183. case ptypesym(sym)^.restype.def^.deftype of
  1184. recorddef :
  1185. st:=precorddef(ptypesym(sym)^.restype.def)^.symtable;
  1186. objectdef :
  1187. st:=pobjectdef(ptypesym(sym)^.restype.def)^.symtable;
  1188. end;
  1189. end;
  1190. typedconstsym :
  1191. begin
  1192. case ptypedconstsym(sym)^.typedconsttype.def^.deftype of
  1193. recorddef :
  1194. st:=precorddef(ptypedconstsym(sym)^.typedconsttype.def)^.symtable;
  1195. objectdef :
  1196. st:=pobjectdef(ptypedconstsym(sym)^.typedconsttype.def)^.symtable;
  1197. end;
  1198. end;
  1199. end;
  1200. end;
  1201. { now walk all recordsymtables }
  1202. while assigned(st) and (s<>'') do
  1203. begin
  1204. { load next field in base }
  1205. i:=pos('.',s);
  1206. if i=0 then
  1207. i:=255;
  1208. base:=Copy(s,1,i-1);
  1209. delete(s,1,i);
  1210. if st^.symtabletype=objectsymtable then
  1211. sym:=search_class_member(pobjectdef(st^.defowner),base)
  1212. else
  1213. sym:=st^.search(base);
  1214. if not assigned(sym) then
  1215. begin
  1216. GetRecordOffsetSize:=false;
  1217. exit;
  1218. end;
  1219. st:=nil;
  1220. case sym^.typ of
  1221. varsym :
  1222. begin
  1223. inc(Offset,pvarsym(sym)^.address);
  1224. Size:=PVarsym(sym)^.getsize;
  1225. case pvarsym(sym)^.vartype.def^.deftype of
  1226. arraydef :
  1227. begin
  1228. { for arrays try to get the element size, take care of
  1229. multiple indexes }
  1230. harrdef:=Parraydef(PVarsym(sym)^.vartype.def);
  1231. while assigned(harrdef^.elementtype.def) and
  1232. (harrdef^.elementtype.def^.deftype=arraydef) do
  1233. harrdef:=parraydef(harrdef^.elementtype.def);
  1234. size:=harrdef^.elesize;
  1235. end;
  1236. recorddef :
  1237. st:=precorddef(pvarsym(sym)^.vartype.def)^.symtable;
  1238. objectdef :
  1239. st:=pobjectdef(pvarsym(sym)^.vartype.def)^.symtable;
  1240. end;
  1241. end;
  1242. end;
  1243. end;
  1244. GetRecordOffsetSize:=(s='');
  1245. end;
  1246. Function SearchLabel(const s: string; var hl: pasmlabel;emit:boolean): boolean;
  1247. var
  1248. sym : psym;
  1249. hs : string;
  1250. Begin
  1251. hl:=nil;
  1252. SearchLabel:=false;
  1253. { Check for pascal labels, which are case insensetive }
  1254. hs:=upper(s);
  1255. getsym(hs,false);
  1256. sym:=srsym;
  1257. if sym=nil then
  1258. exit;
  1259. case sym^.typ of
  1260. labelsym :
  1261. begin
  1262. hl:=plabelsym(sym)^.lab;
  1263. if emit then
  1264. plabelsym(sym)^.defined:=true
  1265. else
  1266. plabelsym(sym)^.used:=true;
  1267. SearchLabel:=true;
  1268. exit;
  1269. end;
  1270. end;
  1271. end;
  1272. {*************************************************************************}
  1273. { Instruction Generation Utilities }
  1274. {*************************************************************************}
  1275. Procedure ConcatString(p : paasmoutput;s:string);
  1276. {*********************************************************************}
  1277. { PROCEDURE ConcatString(s:string); }
  1278. { Description: This routine adds the character chain pointed to in }
  1279. { s to the instruction linked list. }
  1280. {*********************************************************************}
  1281. Var
  1282. pc: PChar;
  1283. Begin
  1284. getmem(pc,length(s)+1);
  1285. p^.concat(new(pai_string,init_length_pchar(strpcopy(pc,s),length(s))));
  1286. end;
  1287. Procedure ConcatPasString(p : paasmoutput;s:string);
  1288. {*********************************************************************}
  1289. { PROCEDURE ConcatPasString(s:string); }
  1290. { Description: This routine adds the character chain pointed to in }
  1291. { s to the instruction linked list, contrary to ConcatString it }
  1292. { uses a pascal style string, so it conserves null characters. }
  1293. {*********************************************************************}
  1294. Begin
  1295. p^.concat(new(pai_string,init(s)));
  1296. end;
  1297. Procedure ConcatDirect(p : paasmoutput;s:string);
  1298. {*********************************************************************}
  1299. { PROCEDURE ConcatDirect(s:string) }
  1300. { Description: This routine output the string directly to the asm }
  1301. { output, it is only sed when writing special labels in AT&T mode, }
  1302. { and should not be used without due consideration, since it may }
  1303. { cause problems. }
  1304. {*********************************************************************}
  1305. Var
  1306. pc: PChar;
  1307. Begin
  1308. getmem(pc,length(s)+1);
  1309. p^.concat(new(pai_direct,init(strpcopy(pc,s))));
  1310. end;
  1311. Procedure ConcatConstant(p: paasmoutput; value: longint; maxvalue: longint);
  1312. {*********************************************************************}
  1313. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1314. { Description: This routine adds the value constant to the current }
  1315. { instruction linked list. }
  1316. { maxvalue -> indicates the size of the data to initialize: }
  1317. { $ff -> create a byte node. }
  1318. { $ffff -> create a word node. }
  1319. { $ffffffff -> create a dword node. }
  1320. {*********************************************************************}
  1321. Begin
  1322. if (maxvalue <> longint($ffffffff)) and (value > maxvalue) then
  1323. Begin
  1324. Message(asmr_e_constant_out_of_bounds);
  1325. { assuming a value of maxvalue }
  1326. value:=maxvalue;
  1327. end;
  1328. if maxvalue = $ff then
  1329. p^.concat(new(pai_const,init_8bit(byte(value))))
  1330. else
  1331. if maxvalue = $ffff then
  1332. p^.concat(new(pai_const,init_16bit(word(value))))
  1333. else
  1334. if maxvalue = longint($ffffffff) then
  1335. p^.concat(new(pai_const,init_32bit(longint(value))));
  1336. end;
  1337. Procedure ConcatConstSymbol(p : paasmoutput;const sym:string;l:longint);
  1338. begin
  1339. p^.concat(new(pai_const_symbol,initname_offset(sym,l)));
  1340. end;
  1341. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  1342. {***********************************************************************}
  1343. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1344. { Description: This routine adds the value constant to the current }
  1345. { instruction linked list. }
  1346. { real_typ -> indicates the type of the real data to initialize: }
  1347. { s32real -> create a single node. }
  1348. { s64real -> create a double node. }
  1349. { s80real -> create an extended node. }
  1350. { s64bit -> create a comp node. }
  1351. { f32bit -> create a fixed node. (not used normally) }
  1352. {***********************************************************************}
  1353. Begin
  1354. case real_typ of
  1355. s32real : p^.concat(new(pai_real_32bit,init(value)));
  1356. s64real : p^.concat(new(pai_real_64bit,init(value)));
  1357. s80real : p^.concat(new(pai_real_80bit,init(value)));
  1358. s64comp : p^.concat(new(pai_comp_64bit,init(value)));
  1359. f32bit : p^.concat(new(pai_const,init_32bit(trunc(value*$10000))));
  1360. end;
  1361. end;
  1362. Procedure ConcatLabel(p: paasmoutput;var l : pasmlabel);
  1363. {*********************************************************************}
  1364. { PROCEDURE ConcatLabel }
  1365. { Description: This routine either emits a label or a labeled }
  1366. { instruction to the linked list of instructions. }
  1367. {*********************************************************************}
  1368. begin
  1369. p^.concat(new(pai_label,init(l)));
  1370. end;
  1371. procedure ConcatAlign(p:paasmoutput;l:longint);
  1372. {*********************************************************************}
  1373. { PROCEDURE ConcatPublic }
  1374. { Description: This routine emits an global definition to the }
  1375. { linked list of instructions.(used by AT&T styled asm) }
  1376. {*********************************************************************}
  1377. begin
  1378. p^.concat(new(pai_align,init(l)));
  1379. end;
  1380. procedure ConcatPublic(p:paasmoutput;const s : string);
  1381. {*********************************************************************}
  1382. { PROCEDURE ConcatPublic }
  1383. { Description: This routine emits an global definition to the }
  1384. { linked list of instructions.(used by AT&T styled asm) }
  1385. {*********************************************************************}
  1386. begin
  1387. p^.concat(new(pai_symbol,initname_global(s,0)));
  1388. end;
  1389. procedure ConcatLocal(p:paasmoutput;const s : string);
  1390. {*********************************************************************}
  1391. { PROCEDURE ConcatLocal }
  1392. { Description: This routine emits an local definition to the }
  1393. { linked list of instructions. }
  1394. {*********************************************************************}
  1395. begin
  1396. p^.concat(new(pai_symbol,initname(s,0)));
  1397. end;
  1398. Procedure ConcatGlobalBss(const s : string;size : longint);
  1399. {*********************************************************************}
  1400. { PROCEDURE ConcatGlobalBss }
  1401. { Description: This routine emits an global datablock to the }
  1402. { linked list of instructions. }
  1403. {*********************************************************************}
  1404. begin
  1405. bsssegment^.concat(new(pai_datablock,init_global(s,size)));
  1406. end;
  1407. Procedure ConcatLocalBss(const s : string;size : longint);
  1408. {*********************************************************************}
  1409. { PROCEDURE ConcatLocalBss }
  1410. { Description: This routine emits a local datablcok to the }
  1411. { linked list of instructions. }
  1412. {*********************************************************************}
  1413. begin
  1414. bsssegment^.concat(new(pai_datablock,init(s,size)));
  1415. end;
  1416. end.
  1417. {
  1418. $Log$
  1419. Revision 1.4 2000-08-27 16:11:52 peter
  1420. * moved some util functions from globals,cobjects to cutils
  1421. * splitted files into finput,fmodule
  1422. Revision 1.3 2000/08/06 10:42:29 peter
  1423. * merged patches name generation in lib and asm constant eval
  1424. Revision 1.2 2000/07/13 11:32:48 michael
  1425. + removed logs
  1426. }