Implementation of SYSTEM.VAL()

Kernel, Loader, code execution and working with memory
Josef Templ
Posts: 262
Joined: Tue Sep 17, 2013 6:50 am

Re: Implementation of SYSTEM.VAL()

Post by Josef Templ »

An even more important problem is the compiler TRAP when compiling the following module:

Code: Select all

MODULE  Test;
  IMPORT SYSTEM;
  VAR x: LONGINT; y: INTEGER;
BEGIN
  x := SYSTEM.VAL(LONGINT, y * 2);
END  Test.
- Josef
luowy
Posts: 87
Joined: Thu Dec 17, 2015 1:32 pm

Re: Implementation of SYSTEM.VAL()

Post by luowy »

Josef Templ wrote:An even more important problem is the compiler TRAP when compiling the following module:

Code: Select all

MODULE  Test;
  IMPORT SYSTEM;
  VAR x: LONGINT; y: INTEGER;
BEGIN
  x := SYSTEM.VAL(LONGINT, y * 2);
END  Test.
- Josef
already done, DevCPC486.ConvMove

Code: Select all

		ELSE
				(*y.form := f;*)
				IF m = Stk THEN
					y.form := f;
					IF ((f < Int32) OR (f = Char16)) & (y.mode # Reg) THEN LoadW(y, hint, stop) END;
					Push(y)
				ELSIF m # Undef THEN
					y.form := f;
					IF f = Int64 THEN
						IF y.mode # Reg THEN LoadLong(y, hint, stop) END;
						Free(y); y.form := Int32; z := x; z.form := Int32; DevCPL486.GenMove(y, z);
						IF z.mode = Reg THEN ASSERT(z.reg # y.index); z.reg := z.index ELSE INC(z.offset, 4) END;
						y.reg := y.index; DevCPL486.GenMove(y, z);
					ELSE
						IF y.mode # Reg THEN LoadW(y, hint, stop) END;
						Free(y); DevCPL486.GenMove(y, x)
					END

				(*ELSIF (y.mode = Stk) & (y.form = Int64) & (f = Int32 )THEN  (*SHORT(ENTIER(real))*)
					LoadLong(y, hint, stop);
					y.form := f; z:=y; z.reg:=z.index; Free(z);*)
				ELSE
					IF (f = Int64) & (y.mode = Reg) & ((y.form <= Int32) OR(y.form=Char16)) THEN
						y.index := 2;
					END;
					y.form := f
				END
			END
		END	
	END ConvMove;
luowy
Josef Templ
Posts: 262
Joined: Tue Sep 17, 2013 6:50 am

Re: Implementation of SYSTEM.VAL()

Post by Josef Templ »

Let us look at the SYSTEM.VAL(LONGINT, int reg) TRAP in the center.
My first impression is that it is not fixed properly and that it cannot be fixed properly.
A single register cannot be re-interpreted as a pair of registers.
This must lead to a compiler error message and this is not related to ConvMove.

- Josef
luowy
Posts: 87
Joined: Thu Dec 17, 2015 1:32 pm

Re: Implementation of SYSTEM.VAL()

Post by luowy »

DevCPB.StPar1

Code: Select all

	| valfn:    ...
				IF (x.class = Nconst) & (x.typ = p.typ) THEN	(* ok *)
				ELSIF (x.class >= Nconst) OR ((f IN realSet) # (p.typ.form IN realSet))
						OR (DevCPM.options * {DevCPM.java, DevCPM.allSysVal} # {}) THEN
					IF (p.typ.comp=Basic)&(x.typ.comp=Basic)&(p.typ.size # x.typ.size) & ((p.typ.size > 4) OR (x.typ.size > 4))THEN 
						DevCPM.err(220) (* 220 illegal value of parameter*);
					ELSE
						t := DevCPT.NewNode(Nmop); t.subcl := val; t.left := x; x := t
					END;
				ELSE x.readonly := FALSE; 
					(**)
				END ;
				x.typ := p.typ; p := x
		| movefn: (*SYSTEM.MOVE*)
luowy
X512
Posts: 72
Joined: Sat Feb 07, 2015 2:51 pm

Re: Implementation of SYSTEM.VAL()

Post by X512 »

Josef Templ wrote:Let us look at the SYSTEM.VAL(LONGINT, int reg) TRAP in the center.
A single register cannot be re-interpreted as a pair of registers.
For x86, EAX register can be interpreted as EDX:EAX for LONGINT.

SYSTEM module services is a low level unsafe services and programmer should clearly understand what he is doing. I think that making safety checks for SYSTEM module is unnecessary. Reinterpreting to type larger than source can be useful in some cases, for example:

Code: Select all

MODULE A;
	IMPORT S := SYSTEM, W := WinApi, Log := StdLog;
	
	PROCEDURE WritePoint (IN pt: W.POINT);
	BEGIN
		Log.Int(pt.x); Log.String(", "); Log.Int(pt.y); Log.Ln;
	END WritePoint;
	
	PROCEDURE Do* (x, y: INTEGER);
	BEGIN
		WritePoint(S.VAL(W.POINT, x));
	END Do;
	
END A.

(!)"A.Do(434, 34434)"
Josef Templ
Posts: 262
Joined: Tue Sep 17, 2013 6:50 am

Re: Implementation of SYSTEM.VAL()

Post by Josef Templ »

The SYSTEM.VAL Traps have been fixed in the center version.
There are no incompatibilities with existing code and your example works well.

- Josef
Josef Templ
Posts: 262
Joined: Tue Sep 17, 2013 6:50 am

Re: Implementation of SYSTEM.VAL()

Post by Josef Templ »

As I already mentioned, the SYSTEM.VAL TRAPs have been solved in the center version.
Unfortunately, Ivan Denisov does not want it to be in the release.
May be some of our russian friends can talk to him (in russian) and convince him that
there is no problem involved with this fix.
The fix only changes the error reporting behavior from trapping to non-trapping.
There is no code change for successful applications of SYSTEM.VAL.

- Josef
User avatar
Ivan Denisov
Posts: 362
Joined: Tue Sep 17, 2013 12:21 am
Location: Krasnoyarsk, Russia

Re: Implementation of SYSTEM.VAL()

Post by Ivan Denisov »

To be honest this bug was fixed earlier by LuoWy and with respect to the work that had been done you can try this fixed version also:
http://blackboxframework.org/unstable/i ... c1.654.zip

And compile this module:

Code: Select all

MODULE  Test;
  IMPORT SYSTEM;
	VAR
	x0: LONGINT; y0: INTEGER;
  x1: BYTE; y1: REAL;
	x2: INTEGER; y2: REAL;
	
BEGIN
x0 := SYSTEM.VAL(LONGINT, y0 * 2);
x1 := SYSTEM.VAL(BYTE, y1 * 2);
x2 := SYSTEM.VAL(INTEGER, y2 * 2);
END  Test.
You will see that it is nice error reporting.

Also please check not bad solution by Josef:
http://blackboxframework.org/unstable/i ... c1.652.zip

All versions have the advantages and disadvantaged, so it is now hard work to choose one.

However I think, that X512 is right, and we should not adopt any of this fixes but think more how to reinterpret registers without TRAP and error message.
Post Reply