Check-in [f612155ba9]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add virtual pointers for 64-bit RatufaCoat which virtualizes some memory.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:f612155ba956f213766ecee2735257c604452e81
User & Date: stephanie.gawroriski 2019-06-16 17:11:28
Context
2019-06-16
17:12
Comment out fprintfs. check-in: cbe4b90bbd user: stephanie.gawroriski tags: trunk
17:11
Add virtual pointers for 64-bit RatufaCoat which virtualizes some memory. check-in: f612155ba9 user: stephanie.gawroriski tags: trunk
15:52
Turn MEM_MARK_INVALID into MEM_SET_INT; Add fallback for MEM_SET and MEM_SET_INT check-in: 3437cabeb6 user: stephanie.gawroriski tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to ratufacoat/sjmerc.c.

16
17
18
19
20
21
22









23
24
25
26
27
28
29
...
520
521
522
523
524
525
526











527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591

592
593
594
595
596
597
598
...
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
...
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634

635
636
637
638
























639
640
641
642

643





644
645
646
647
648
649
650
...
651
652
653
654
655
656
657



658

659
660
661
662
663
664
665
...
685
686
687
688
689
690
691



692

693
694
695
696
697
698
699
700
701
702
703
704
705
706
...
730
731
732
733
734
735
736

737

738
739
740



741

742



743
744
745

746
747

748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770




771
772
773
774
775
776
777
...
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827




828
829
830
831
832
833
834
...
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
...
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
....
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
....
1484
1485
1486
1487
1488
1489
1490




















1491
1492
1493
1494
1495
1496

1497
1498
1499
1500
1501
1502
1503
....
1618
1619
1620
1621
1622
1623
1624
1625

1626

1627
1628
1629
1630
1631
1632
1633
....
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
....
1898
1899
1900
1901
1902
1903
1904
1905
1906

1907
1908
1909
1910
1911











1912
1913
1914
1915
1916
1917
1918
....
1927
1928
1929
1930
1931
1932
1933


1934
1935
1936
1937
1938
1939
1940
1941
....
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
....
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
....
2254
2255
2256
2257
2258
2259
2260



2261
2262
2263
2264
2265
2266
2267
....
2272
2273
2274
2275
2276
2277
2278







2279
2280
2281
2282
2283
2284
2285
....
2296
2297
2298
2299
2300
2301
2302







2303
2304
2305
2306
2307
2308
2309
....
2363
2364
2365
2366
2367
2368
2369







2370
2371
2372
2373






2374
2375
2376
2377
2378
2379
2380
....
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397

/** Default RAM size. */
#if defined(SJME_IS_DOS)
	#define SJME_DEFAULT_RAM_SIZE SJME_JINT_C(65536)
#else
	#define SJME_DEFAULT_RAM_SIZE SJME_JINT_C(16777216)
#endif










/** Default size of configuration ROM. */
#define SJME_DEFAULT_CONF_SIZE SJME_JINT_C(65536)

/** Magic number for ROMs. */
#define SJME_ROM_MAGIC_NUMBER SJME_JINT_C(0x58455223)

................................................................................
	sjme_jint fairthreadid;
	
	/** Threads. */
	sjme_cpu threads[SJME_THREAD_MAX];
	
	/* Total instruction count. */
	sjme_jint totalinstructions;











};

/**
 * Allocates the given number of bytes.
 *
 * @param size The number of bytes to allocate.
 * @since 2019/06/07
 */
void* sjme_malloc(sjme_jint size)
{
#if defined(__WATCOMC__) && defined(SJME_IS_DOS)
	sjme_jint c, d;
	void* vp;
#endif
	void* rv;
	
	/* These will never allocate. */
	if (size <= 0)
		return NULL;
	
	/* Round size and include extra 4-bytes for size storage. */
	size = ((size + SJME_JINT_C(3)) & (~SJME_JINT_C(3))) + SJME_JINT_C(4);
	
#if defined(SJME_IS_LINUX) && SJME_BITS > 32
	/* Use memory map on Linux so it is forced to a 32-bit pointer. */
	rv = mmap(NULL, size, PROT_READ | PROT_WRITE,
		MAP_PRIVATE | MAP_ANONYMOUS | MAP_32BIT, -1, 0);
	if (rv == MAP_FAILED)
		return NULL;
#elif defined(__WATCOMC__) && defined(SJME_IS_DOS)
	/* Watcom C has huge memory allocation. Since normal malloc is limited */
	/* to 65K we need to actually claim more than this! So halloc(n, s) */
	/* allocates n*s bytes (where s is size_t). Note that the result will */
	/* be aligned to the second argument. The second argument must be a */
	/* power of two. */
	d = 4;
	c = size / d;
	if (c <= 0)
		c = 1;
	rv = halloc(c, d);
	if (rv == NULL)
		return NULL;
	
	/* Wipe it. */
	vp = rv;
	for (c = size; c > 0; c -= SJME_JINT_C(65535))
	{
		/* Bytes to clear at a time. */
		d = (c > SJME_JINT_C(65535) ? SJME_JINT_C(65535) : c);
		
		/* Clear these bytes. */
		memset(vp, 0, d);
		
		/* Move up. */
		vp = SJME_POINTER_OFFSET(vp, d);
	}
#else
	/* Exceeds maximum permitted allocation size? */
	if (sizeof(sjme_jint) > sizeof(size_t) && size > (sjme_jint)SIZE_MAX)
		return NULL;
	
	/* Use standard C function otherwise. */
	rv = calloc(1, size);
	
	/* Address out of range? */

	if (rv != NULL && ((uintptr_t)rv) > (uintptr_t)UINT32_C(0xFFFFFFFF))
	{
		free(rv);
		return NULL;
	}
#endif

................................................................................
	if (rv == NULL)
		return NULL;
	
	/* Store the size into this memory block for later free. */
	*((sjme_jint*)rv) = size;
	
	/* Return the adjusted pointer. */
	return SJME_POINTER_OFFSET(rv, 4);
}

/**
 * Frees the given pointer.
 *
 * @param p The pointer to free.
 * @since 2019/06/07
................................................................................
	sjme_jint size;
	
	/* Ignore null pointers. */
	if (p == NULL)
		return;
	
	/* Base pointer which is size shifted. */
	basep = SJME_POINTER_OFFSET(p, -4);
	
	/* Read size. */
	size = *((sjme_jint*)basep);

#if defined(SJME_IS_LINUX) && SJME_BITS > 32
	/* Remove the memory mapping. */
	munmap(basep, size);
	

#elif defined(__WATCOMC__) && defined(SJME_IS_DOS)
	/* Watcom Huge Free. */
	hfree(basep);
	
























#else
	/* Use Standard C free. */
	free(basep);
#endif

}






/**
 * Reads a value from memory.
 *
 * @param jvm JVM pointer.
 * @param size The size of the value to read.
 * @param ptr The pointer to read from.
................................................................................
 * @param off The offset.
 * @return The read value.
 * @since 2019/06/08
 */
sjme_jint sjme_memread(sjme_jvm* jvm, sjme_jint size, void* ptr, sjme_jint off)
{
	/* Get the true pointer. */



	ptr = SJME_POINTER_OFFSET(ptr, off);

	
	/* Read value from memory. */
	switch (size)
	{
			/* Byte */
		case 1:
			return *((sjme_jbyte*)ptr);
................................................................................
 * @param value The value to write.
 * @since 2019/06/08
 */
void sjme_memwrite(sjme_jvm* jvm, sjme_jint size, void* ptr, sjme_jint off,
	sjme_jint value)
{
	/* Get the true pointer. */



	ptr = SJME_POINTER_OFFSET(ptr, off);

	
	/* Check against JVM areas. */
	if (jvm != NULL)
	{
		/* Ignore writes to ROM. */
		if (ptr >= jvm->rom &&
			ptr < SJME_POINTER_OFFSET(jvm->rom, jvm->romsize))
			return;
	}
	
	/* Write value to memory. */
	switch (size)
	{
			/* Byte */
................................................................................
 * @param off The offset.
 * @return The read value.
 * @since 2019/06/08
 */
sjme_jint sjme_memjread(sjme_jvm* jvm, sjme_jint size, void* ptr,
	sjme_jint off)
{

	sjme_jint rv;

	
	/* Get the true pointer. */
	ptr = SJME_POINTER_OFFSET(ptr, off);



	

	/* Read value from memory. */



	switch (size)
	{
			/* Byte */

		case 1:
			return *((sjme_jbyte*)ptr);

		
			/* Short */
		case 2:
			rv = *((sjme_jshort*)ptr);
#if defined(SJME_LITTLE_ENDIAN)
			rv = (rv & SJME_JINT_C(0xFFFF0000)) |
				(((rv << SJME_JINT_C(8)) & SJME_JINT_C(0xFF00)) |
				((rv >> SJME_JINT_C(8)) & SJME_JINT_C(0x00FF)));
#endif
			return rv;
			
			/* Integer */
		case 4:
		default:
			rv = *((sjme_jint*)ptr);
#if defined(SJME_LITTLE_ENDIAN)
			rv = (((rv >> SJME_JINT_C(24)) & SJME_JINT_C(0x000000FF)) |
				((rv >> SJME_JINT_C(8)) & SJME_JINT_C(0x0000FF00)) |
				((rv << SJME_JINT_C(8)) & SJME_JINT_C(0x00FF0000)) |
				((rv << SJME_JINT_C(24)) & SJME_JINT_C(0xFF000000)));
#endif
			return rv;
	}




}

/**
 * Writes a Java value to memory.
 *
 * @param jvm JVM pointer.
 * @param size The size of the value to write.
................................................................................
 * @param off The offset.
 * @param value The value to write.
 * @since 2019/06/14
 */
void sjme_memjwrite(sjme_jvm* jvm, sjme_jint size, void* ptr, sjme_jint off,
	sjme_jint value)
{
	/* Get the true pointer. */
	ptr = SJME_POINTER_OFFSET(ptr, off);
	
	/* Check against JVM areas. */
	if (jvm != NULL)
	{
		/* Ignore writes to ROM. */
		if (ptr >= jvm->rom &&
			ptr < SJME_POINTER_OFFSET(jvm->rom, jvm->romsize))
			return;
	}
	
	/* Write value to memory. */
	switch (size)
	{
			/* Byte */
		case 1:
			*((sjme_jbyte*)ptr) = (sjme_jbyte)value;
			break;
		
			/* Short */
		case 2:
#if defined(SJME_LITTLE_ENDIAN)
			value = (value & SJME_JINT_C(0xFFFF0000)) |
				(((value << SJME_JINT_C(8)) & SJME_JINT_C(0xFF00)) |
				((value >> SJME_JINT_C(8)) & SJME_JINT_C(0x00FF)));
#endif
			*((sjme_jshort*)ptr) = (sjme_jshort)value;
			break;
			
			/* Integer */
		case 4:
		default:
#if defined(SJME_LITTLE_ENDIAN)
			value = (((value >> SJME_JINT_C(24)) & SJME_JINT_C(0x000000FF)) |
				((value >> SJME_JINT_C(8)) & SJME_JINT_C(0x0000FF00)) |
				((value << SJME_JINT_C(8)) & SJME_JINT_C(0x00FF0000)) |
				((value << SJME_JINT_C(24)) & SJME_JINT_C(0xFF000000)));
#endif
			*((sjme_jint*)ptr) = value;
			break;
	}




}

/**
 * Read Java value from memory and increment pointer.
 *
 * @param jvm The JVM.
 * @param size The size of value to read.
................................................................................
{
	sjme_jint rv;
	
	/* Read pointer value. */
	rv = sjme_memjread(jvm, size, *ptr, 0);
	
	/* Increment pointer. */
	*ptr = SJME_POINTER_OFFSET(*ptr, size);
	
	/* Return result. */
	return rv;
}

/**
 * Write Java value to memory and increment pointer.
................................................................................
void sjme_memjwritep(sjme_jvm* jvm, sjme_jint size, void** ptr,
	sjme_jint value)
{
	/* Write pointer value. */
	sjme_memjwrite(jvm, size, *ptr, 0, value);
	
	/* Increment pointer. */
	*ptr = SJME_POINTER_OFFSET(*ptr, size);
}

/**
 * Decodes a variable unsigned int operation argument.
 *
 * @param ptr The pointer to read from.
 * @return The resulting decoded value.
................................................................................
#if 1
		fprintf(stderr,
			"ti=%d pc=%p op=%X cl=%s mn=%s mt=%s ln=%d jo=%x ja=%d\n",
			jvm->totalinstructions,
			cpu->pc,
			(unsigned int)op,
			(cpu->debugclassname == NULL ? NULL :
				SJME_POINTER_OFFSET(cpu->debugclassname, 2)),
			(cpu->debugmethodname == NULL ? NULL :
				SJME_POINTER_OFFSET(cpu->debugmethodname, 2)),
			(cpu->debugmethodtype == NULL ? NULL :
				SJME_POINTER_OFFSET(cpu->debugmethodtype, 2)),
			(int)cpu->debugline,
			(unsigned int)cpu->debugjop,
			(int)cpu->debugjpc);
#endif
		
		/* Depends on the operation. */
		switch (enc)
................................................................................
				break;
			
				/* Debug entry. */
			case SJME_OP_DEBUG_ENTRY:
				{
					tempp = SJME_JINT_TO_POINTER(r[SJME_POOL_REGISTER]);
					




















					cpu->debugclassname = SJME_JINT_TO_POINTER(
						((sjme_jint*)tempp)[sjme_opdecodeui(jvm, &nextpc)]);
					cpu->debugmethodname = SJME_JINT_TO_POINTER(
						((sjme_jint*)tempp)[sjme_opdecodeui(jvm, &nextpc)]);
					cpu->debugmethodtype = SJME_JINT_TO_POINTER(
						((sjme_jint*)tempp)[sjme_opdecodeui(jvm, &nextpc)]);

				}
				break;
				
				/* Exit method. */
			case SJME_OP_DEBUG_EXIT:
				break;
				
................................................................................
				/* Load value from constant pool. */
			case SJME_OP_LOAD_POOL:
				{
					/* The index to read from. */
					ia = sjme_opdecodeui(jvm, &nextpc);
					
					/* Write into destination register. */
					r[sjme_opdecodeui(jvm, &nextpc)] = ((sjme_jint*)

						SJME_JINT_TO_POINTER(r[SJME_POOL_REGISTER]))[ia];

				}
				break;
				
				/* Return from method. */
			case SJME_OP_RETURN:
				{
					/* Get parent CPU state. */
................................................................................
		if (rv != NULL)
		{
			/* Read whatever is possible. */
			for (readat = 0; readat < romsize;)
			{
				/* Read into raw memory. */
				readcount = nativefuncs->fileread(file,
					SJME_POINTER_OFFSET(rv, readat), romsize - readat,
					&xerror);
				
				/* EOF or error? */
				if (readcount < 0)
				{
					/* End of file reached? */
					if (xerror == SJME_ERROR_ENDOFFILE)
................................................................................
 * @since 2019/06/07
 */
sjme_jint sjme_initboot(void* rom, void* ram, sjme_jint ramsize, sjme_jvm* jvm,
	sjme_jint* error)
{
	void* rp;
	void* bootjar;
	sjme_jbyte* byteram;
	sjme_jint bootoff, i, n, seedop, seedaddr, seedvalh, seedvall, seedsize;

	sjme_cpu* cpu;
	
	/* Invalid arguments. */
	if (rom == NULL || ram == NULL || ramsize <= 0 || jvm == NULL)
		return 0;











	
	/* Set initial CPU (the first). */
	cpu = &jvm->threads[0];
	cpu->state = SJME_THREAD_STATE_RUNNING;
	
	/* Set boot pointer to start of ROM. */
	rp = rom;
................................................................................
	}
	
	/* Ignore JAR count and BootJAR index. */
	sjme_memjreadp(jvm, 4, &rp);
	sjme_memjreadp(jvm, 4, &rp);
	
	/* Read and calculate BootJAR position. */


	rp = bootjar = SJME_POINTER_OFFSET(rom, sjme_memjreadp(jvm, 4, &rp));
	
	/* Check JAR magic number. */
	if (sjme_memjreadp(jvm, 4, &rp) != SJME_JAR_MAGIC_NUMBER)
	{
		if (error != NULL)
			*error = SJME_ERROR_INVALIDJARMAGIC;
		
................................................................................
	/* Read boot offset for later. */
	bootoff = sjme_memjreadp(jvm, 4, &rp);
	
	/* Ignore bootsize. */
	sjme_memjreadp(jvm, 4, &rp);
	
	/* Seed initial CPU state. */
	cpu->r[SJME_POOL_REGISTER] = SJME_POINTER_TO_JINT(
		SJME_POINTER_OFFSET(ram, sjme_memjreadp(jvm, 4, &rp)));
	cpu->r[SJME_STATIC_FIELD_REGISTER] = SJME_POINTER_TO_JINT(
		SJME_POINTER_OFFSET(ram, sjme_memjreadp(jvm, 4, &rp)));
	cpu->pc = SJME_POINTER_OFFSET(bootjar, sjme_memjreadp(jvm, 4, &rp));
	
	/* Bootstrap entry arguments. */
	/* (int __rambase, int __ramsize, int __rombase, int __romsize, */
	/* int __confbase, int __confsize) */
	cpu->r[SJME_ARGBASE_REGISTER + 0] = SJME_POINTER_TO_JINT(ram);
	cpu->r[SJME_ARGBASE_REGISTER + 1] = ramsize;
	cpu->r[SJME_ARGBASE_REGISTER + 2] = SJME_POINTER_TO_JINT(rom);
	cpu->r[SJME_ARGBASE_REGISTER + 3] = jvm->romsize;
	cpu->r[SJME_ARGBASE_REGISTER + 4] = SJME_POINTER_TO_JINT(jvm->config);
	cpu->r[SJME_ARGBASE_REGISTER + 5] = jvm->configsize;
	
	/* Address where the BootRAM is read from. */
	rp = SJME_POINTER_OFFSET(bootjar, bootoff);
	
	/* Copy initial base memory bytes, which is pure big endian. */
	byteram = (sjme_jbyte*)ram;
	n = sjme_memjreadp(jvm, 4, &rp);
	for (i = 0; i < n; i++)
		byteram[i] = sjme_memjreadp(jvm, 1, &rp);
	
	/* Load all seeds, which restores natural byte order. */
	n = sjme_memjreadp(jvm, 4, &rp);
	for (i = 0; i < n; i++)
	{
		/* Read seed information. */
		seedop = sjme_memjreadp(jvm, 1, &rp);
................................................................................
				*error = SJME_ERROR_INVALIDBOOTRAMSEED;
			
			return 0;
		}
		
		/* Offset value if it is in RAM or JAR ROM. */
		if (seedop == 1)
			seedvalh += SJME_POINTER_TO_JINT(ram);
		else if (seedop == 2)
			seedvalh += SJME_POINTER_TO_JINT(bootjar);
		
		/* Write long value. */
		if (seedsize == 8)
		{
#if defined(SJME_BIG_ENDIAN)
			sjme_memwrite(jvm, 4, ram, seedaddr, seedvalh);
			sjme_memwrite(jvm, 4, ram, seedaddr + SJME_JINT_C(4), seedvall);
................................................................................
{
	sjme_jvmoptions nulloptions;
	void* ram;
	void* rom;
	void* conf;
	sjme_jvm* rv;
	sjme_jint i, l, romsize;



	
	/* We need native functions. */
	if (nativefuncs == NULL)
		return NULL;
	
	/* Allocate VM state. */
	rv = sjme_malloc(sizeof(*rv));
................................................................................
			*error = SJME_ERROR_NOMEMORY;
		
		sjme_free(rv);
		sjme_free(conf);
		
		return NULL;
	}







	
	/* If there were no options specified, just use a null set. */
	if (options == NULL)
	{
		memset(&nulloptions, 0, sizeof(nulloptions));
		options = &nulloptions;
	}
................................................................................
			*error = SJME_ERROR_NOMEMORY;
			
		sjme_free(rv);
		sjme_free(conf);
		
		return NULL;
	}







	
	/* Needed by the VM. */
	rv->ram = ram;
	rv->ramsize = options->ramsize;
	rv->nativefuncs = nativefuncs;
	
	/* Needed by destruction later. */
................................................................................
			/* Offset up. */
			i += l;
		}
		
		/* We copied it, so never make a preset ROM. */
		rv->presetrom = NULL;
	}







	
	/* Set JVM rom space. */
	rv->rom = rom;
	rv->romsize = romsize;






	
	/* Initialize the BootRAM and boot the CPU. */
	if (sjme_initboot(rom, ram, options->ramsize, rv, error) == 0)
	{
		sjme_free(rv);
		sjme_free(ram);
		sjme_free(conf);
................................................................................
		/* If a pre-set ROM is not being used, make sure it gets cleared. */
		if (options->presetrom)
			sjme_free(rom);
		
		return NULL;
	}
	
	/* Initialize configuration space. */
	rv->config = conf;
	rv->configsize = SJME_DEFAULT_CONF_SIZE;
	sjme_configinit(conf, SJME_DEFAULT_CONF_SIZE, rv, options, nativefuncs,
		error);
	
	/* The JVM is ready to use. */
	return rv;
}







>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>










<
<
<
<









<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







|
>







 







|







 







|



|
|
<
|
|
>
|
|
|
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
<
<
>
|
>
>
>
>
>







 







>
>
>

>







 







>
>
>

>






|







 







>

>

<
<
>
>
>
|
>
|
>
>
>


<
>

<
>
|


<
<



|
<




<
<




|
<

>
>
>
>







 







|
|
<
<
<
<
<
<
<
<
<
<
<




<




<



<
<





<




<
<


>
>
>
>







 







|







 







|







 







|

|

|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
<
|
<
|
<
>







 







|
>
|
>







 







|







 







|

>





>
>
>
>
>
>
>
>
>
>
>







 







>
>
|







 







|
|
|
|
|




|

|

|



|


|


|







 







|

|







 







>
>
>







 







>
>
>
>
>
>
>







 







>
>
>
>
>
>
>







 







>
>
>
>
>
>
>




>
>
>
>
>
>







 







<
<
<
<
<
<



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
...
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556




557
558
559
560
561
562
563
564
565


































566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
...
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
...
602
603
604
605
606
607
608
609
610
611
612
613
614

615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646



647
648
649
650
651
652
653
654
655
656
657
658
659
660
...
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
...
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
...
748
749
750
751
752
753
754
755
756
757
758


759
760
761
762
763
764
765
766
767
768
769

770
771

772
773
774
775


776
777
778
779

780
781
782
783


784
785
786
787
788

789
790
791
792
793
794
795
796
797
798
799
800
...
802
803
804
805
806
807
808
809
810











811
812
813
814

815
816
817
818

819
820
821


822
823
824
825
826

827
828
829
830


831
832
833
834
835
836
837
838
839
840
841
842
843
...
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
...
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
....
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
....
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520

1521

1522

1523
1524
1525
1526
1527
1528
1529
1530
....
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
....
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
....
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
....
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
....
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
....
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
....
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
....
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
....
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
....
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
....
2455
2456
2457
2458
2459
2460
2461






2462
2463
2464

/** Default RAM size. */
#if defined(SJME_IS_DOS)
	#define SJME_DEFAULT_RAM_SIZE SJME_JINT_C(65536)
#else
	#define SJME_DEFAULT_RAM_SIZE SJME_JINT_C(16777216)
#endif

/** Virtual memory stuff. */
#if defined(SJME_VIRTUAL_MEM)
	/** Base of virtual memory. */
	#define SJME_VIRTUAL_MEM_BASE SJME_JINT_C(1048576)
	
	/** Rounding of virtual memory. */
	#define SJME_VIRTUAL_MEM_MASK SJME_JINT_C(1023)
#endif

/** Default size of configuration ROM. */
#define SJME_DEFAULT_CONF_SIZE SJME_JINT_C(65536)

/** Magic number for ROMs. */
#define SJME_ROM_MAGIC_NUMBER SJME_JINT_C(0x58455223)

................................................................................
	sjme_jint fairthreadid;
	
	/** Threads. */
	sjme_cpu threads[SJME_THREAD_MAX];
	
	/* Total instruction count. */
	sjme_jint totalinstructions;
	
#if defined(SJME_VIRTUAL_MEM)
	/** Base address for Configuration ROM. */
	sjme_jint configbase;
	
	/** Base address for RAM. */
	sjme_jint rambase;
	
	/** Base address for ROM. */
	sjme_jint rombase;
#endif
};

/**
 * Allocates the given number of bytes.
 *
 * @param size The number of bytes to allocate.
 * @since 2019/06/07
 */
void* sjme_malloc(sjme_jint size)
{




	void* rv;
	
	/* These will never allocate. */
	if (size <= 0)
		return NULL;
	
	/* Round size and include extra 4-bytes for size storage. */
	size = ((size + SJME_JINT_C(3)) & (~SJME_JINT_C(3))) + SJME_JINT_C(4);
	


































	/* Exceeds maximum permitted allocation size? */
	if (sizeof(sjme_jint) > sizeof(size_t) && size > (sjme_jint)SIZE_MAX)
		return NULL;
	
	/* Use standard C function otherwise. */
	rv = calloc(1, size);
	
	/* Address out of range? Only when virtual pointers are not used! */
#if !defined(SJME_VIRTUAL_MEM)
	if (rv != NULL && ((uintptr_t)rv) > (uintptr_t)UINT32_C(0xFFFFFFFF))
	{
		free(rv);
		return NULL;
	}
#endif

................................................................................
	if (rv == NULL)
		return NULL;
	
	/* Store the size into this memory block for later free. */
	*((sjme_jint*)rv) = size;
	
	/* Return the adjusted pointer. */
	return SJME_POINTER_OFFSET_LONG(rv, 4);
}

/**
 * Frees the given pointer.
 *
 * @param p The pointer to free.
 * @since 2019/06/07
................................................................................
	sjme_jint size;
	
	/* Ignore null pointers. */
	if (p == NULL)
		return;
	
	/* Base pointer which is size shifted. */
	basep = SJME_POINTER_OFFSET_LONG(p, -4);
	
	/* Read size. */
	size = *((sjme_jint*)basep);
	
	/* Use Standard C free. */

	free(basep);
}

#if defined(SJME_VIRTUAL_MEM)
/** Returns the real pointer for the given virtual pointer. */
void* sjme_realptr(sjme_jvm* jvm, void* ptr)
{
	void* oldptr;
	sjme_jint vptr;
	
	/* Pass through memory if no JVM specified. */
	if (jvm == NULL)
		return ptr;
	
	/* Map pointer to pure integer. */
	oldptr = ptr;
	vptr = SJME_POINTER_TO_JINT(oldptr);
	
	/* Within Configuration ROM region? */
	if (vptr >= jvm->configbase && vptr < jvm->configbase + jvm->configsize)
		ptr = SJME_POINTER_OFFSET_LONG(jvm->config, (vptr - jvm->configbase));
	
	/* Within RAM region? */
	else if (vptr >= jvm->rambase && vptr < jvm->rambase + jvm->ramsize)
		ptr = SJME_POINTER_OFFSET_LONG(jvm->ram, (vptr - jvm->rambase));
	
	/* Within ROM region? */
	else if (vptr >= jvm->rombase && vptr < jvm->rombase + jvm->romsize)
		ptr = SJME_POINTER_OFFSET_LONG(jvm->rom, (vptr - jvm->rombase));
	
	/* No mapping found, pass through. */
	else



		ptr = oldptr;
	
	fprintf(stderr, "VPtr %p -> %p\n", oldptr, ptr);
	
	return ptr;
}
#endif

/**
 * Reads a value from memory.
 *
 * @param jvm JVM pointer.
 * @param size The size of the value to read.
 * @param ptr The pointer to read from.
................................................................................
 * @param off The offset.
 * @return The read value.
 * @since 2019/06/08
 */
sjme_jint sjme_memread(sjme_jvm* jvm, sjme_jint size, void* ptr, sjme_jint off)
{
	/* Get the true pointer. */
#if defined(SJME_VIRTUAL_MEM)
	ptr = SJME_POINTER_OFFSET_LONG(sjme_realptr(jvm, ptr), off);
#else
	ptr = SJME_POINTER_OFFSET(ptr, off);
#endif
	
	/* Read value from memory. */
	switch (size)
	{
			/* Byte */
		case 1:
			return *((sjme_jbyte*)ptr);
................................................................................
 * @param value The value to write.
 * @since 2019/06/08
 */
void sjme_memwrite(sjme_jvm* jvm, sjme_jint size, void* ptr, sjme_jint off,
	sjme_jint value)
{
	/* Get the true pointer. */
#if defined(SJME_VIRTUAL_MEM)
	ptr = SJME_POINTER_OFFSET_LONG(sjme_realptr(jvm, ptr), off);
#else
	ptr = SJME_POINTER_OFFSET(ptr, off);
#endif
	
	/* Check against JVM areas. */
	if (jvm != NULL)
	{
		/* Ignore writes to ROM. */
		if (ptr >= jvm->rom &&
			ptr < SJME_POINTER_OFFSET_LONG(jvm->rom, jvm->romsize))
			return;
	}
	
	/* Write value to memory. */
	switch (size)
	{
			/* Byte */
................................................................................
 * @param off The offset.
 * @return The read value.
 * @since 2019/06/08
 */
sjme_jint sjme_memjread(sjme_jvm* jvm, sjme_jint size, void* ptr,
	sjme_jint off)
{
#if !defined(SJME_BIG_ENDIAN)
	sjme_jint rv;
#endif
	


#if defined(SJME_BIG_ENDIAN)
	/* No swapping needed. */
	return sjme_memread(jvm, size, ptr, off);

#else
	/* Read data. */
	rv = sjme_memread(jvm, size, ptr, off);
	
	/* Perform byte swapping. */
	switch (size)
	{

			/* Ignore byte. */
		case 1:

			break;
			
			/* Short */
		case 2:


			rv = (rv & SJME_JINT_C(0xFFFF0000)) |
				(((rv << SJME_JINT_C(8)) & SJME_JINT_C(0xFF00)) |
				((rv >> SJME_JINT_C(8)) & SJME_JINT_C(0x00FF)));
			break;

			
			/* Integer */
		case 4:
		default:


			rv = (((rv >> SJME_JINT_C(24)) & SJME_JINT_C(0x000000FF)) |
				((rv >> SJME_JINT_C(8)) & SJME_JINT_C(0x0000FF00)) |
				((rv << SJME_JINT_C(8)) & SJME_JINT_C(0x00FF0000)) |
				((rv << SJME_JINT_C(24)) & SJME_JINT_C(0xFF000000)));
			break;

	}
	
	/* Use value. */
	return rv;
#endif
}

/**
 * Writes a Java value to memory.
 *
 * @param jvm JVM pointer.
 * @param size The size of the value to write.
................................................................................
 * @param off The offset.
 * @param value The value to write.
 * @since 2019/06/14
 */
void sjme_memjwrite(sjme_jvm* jvm, sjme_jint size, void* ptr, sjme_jint off,
	sjme_jint value)
{
#if defined(SJME_LITTLE_ENDIAN)
	/* Perform byte swapping? */











	switch (size)
	{
			/* Byte */
		case 1:

			break;
		
			/* Short */
		case 2:

			value = (value & SJME_JINT_C(0xFFFF0000)) |
				(((value << SJME_JINT_C(8)) & SJME_JINT_C(0xFF00)) |
				((value >> SJME_JINT_C(8)) & SJME_JINT_C(0x00FF)));


			break;
			
			/* Integer */
		case 4:
		default:

			value = (((value >> SJME_JINT_C(24)) & SJME_JINT_C(0x000000FF)) |
				((value >> SJME_JINT_C(8)) & SJME_JINT_C(0x0000FF00)) |
				((value << SJME_JINT_C(8)) & SJME_JINT_C(0x00FF0000)) |
				((value << SJME_JINT_C(24)) & SJME_JINT_C(0xFF000000)));


			break;
	}
#endif

	/* Write to memory. */
	sjme_memwrite(jvm, size, ptr, off, value);
}

/**
 * Read Java value from memory and increment pointer.
 *
 * @param jvm The JVM.
 * @param size The size of value to read.
................................................................................
{
	sjme_jint rv;
	
	/* Read pointer value. */
	rv = sjme_memjread(jvm, size, *ptr, 0);
	
	/* Increment pointer. */
	*ptr = SJME_POINTER_OFFSET_LONG(*ptr, size);
	
	/* Return result. */
	return rv;
}

/**
 * Write Java value to memory and increment pointer.
................................................................................
void sjme_memjwritep(sjme_jvm* jvm, sjme_jint size, void** ptr,
	sjme_jint value)
{
	/* Write pointer value. */
	sjme_memjwrite(jvm, size, *ptr, 0, value);
	
	/* Increment pointer. */
	*ptr = SJME_POINTER_OFFSET_LONG(*ptr, size);
}

/**
 * Decodes a variable unsigned int operation argument.
 *
 * @param ptr The pointer to read from.
 * @return The resulting decoded value.
................................................................................
#if 1
		fprintf(stderr,
			"ti=%d pc=%p op=%X cl=%s mn=%s mt=%s ln=%d jo=%x ja=%d\n",
			jvm->totalinstructions,
			cpu->pc,
			(unsigned int)op,
			(cpu->debugclassname == NULL ? NULL :
				SJME_POINTER_OFFSET_LONG(cpu->debugclassname, 2)),
			(cpu->debugmethodname == NULL ? NULL :
				SJME_POINTER_OFFSET_LONG(cpu->debugmethodname, 2)),
			(cpu->debugmethodtype == NULL ? NULL :
				SJME_POINTER_OFFSET_LONG(cpu->debugmethodtype, 2)),
			(int)cpu->debugline,
			(unsigned int)cpu->debugjop,
			(int)cpu->debugjpc);
#endif
		
		/* Depends on the operation. */
		switch (enc)
................................................................................
				break;
			
				/* Debug entry. */
			case SJME_OP_DEBUG_ENTRY:
				{
					tempp = SJME_JINT_TO_POINTER(r[SJME_POOL_REGISTER]);
					
					/* Get pointers to the real values. */
					ia = sjme_memread(jvm, 4, tempp,
						sjme_opdecodeui(jvm, &nextpc) * SJME_JINT_C(4));
					ib = sjme_memread(jvm, 4, tempp,
						sjme_opdecodeui(jvm, &nextpc) * SJME_JINT_C(4));
					ic = sjme_memread(jvm, 4, tempp,
						sjme_opdecodeui(jvm, &nextpc) * SJME_JINT_C(4));

					fprintf(stderr, "pool=%08x\n", r[SJME_POOL_REGISTER]);
					fprintf(stderr, "%08x %08x %08x\n", ia, ib, ic); 
#if defined(SJME_VIRTUAL_MEM)
					/* Map to real memory addresses. */
					cpu->debugclassname = sjme_realptr(jvm,
						SJME_JINT_TO_POINTER(ia));
					cpu->debugmethodname = sjme_realptr(jvm,
						SJME_JINT_TO_POINTER(ib));
					cpu->debugmethodtype = sjme_realptr(jvm,
						SJME_JINT_TO_POINTER(ic));
#else
					/* Just forward the values. */
					cpu->debugclassname = SJME_JINT_TO_POINTER(ia);

					cpu->debugmethodname = SJME_JINT_TO_POINTER(ib);

					cpu->debugmethodtype = SJME_JINT_TO_POINTER(ic);

#endif
				}
				break;
				
				/* Exit method. */
			case SJME_OP_DEBUG_EXIT:
				break;
				
................................................................................
				/* Load value from constant pool. */
			case SJME_OP_LOAD_POOL:
				{
					/* The index to read from. */
					ia = sjme_opdecodeui(jvm, &nextpc);
					
					/* Write into destination register. */
					r[sjme_opdecodeui(jvm, &nextpc)] = 
						sjme_memread(jvm, 4,
							SJME_JINT_TO_POINTER(r[SJME_POOL_REGISTER]),
							ia * SJME_JINT_C(4));
				}
				break;
				
				/* Return from method. */
			case SJME_OP_RETURN:
				{
					/* Get parent CPU state. */
................................................................................
		if (rv != NULL)
		{
			/* Read whatever is possible. */
			for (readat = 0; readat < romsize;)
			{
				/* Read into raw memory. */
				readcount = nativefuncs->fileread(file,
					SJME_POINTER_OFFSET_LONG(rv, readat), romsize - readat,
					&xerror);
				
				/* EOF or error? */
				if (readcount < 0)
				{
					/* End of file reached? */
					if (xerror == SJME_ERROR_ENDOFFILE)
................................................................................
 * @since 2019/06/07
 */
sjme_jint sjme_initboot(void* rom, void* ram, sjme_jint ramsize, sjme_jvm* jvm,
	sjme_jint* error)
{
	void* rp;
	void* bootjar;
	void* byteram;
	sjme_jint bootoff, i, n, seedop, seedaddr, seedvalh, seedvall, seedsize;
	sjme_jint bootjaroff, vbootjarbase, vrambase, vrombase, vconfigbase;
	sjme_cpu* cpu;
	
	/* Invalid arguments. */
	if (rom == NULL || ram == NULL || ramsize <= 0 || jvm == NULL)
		return 0;
	
	/* Determine the address the VM sees for some memory types. */
#if defined(SJME_VIRTUAL_MEM)
	vrambase = jvm->rambase;
	vrombase = jvm->rombase;
	vconfigbase = jvm->configbase;
#else
	vrambase = SJME_POINTER_TO_JINT(jvm->ram);
	vrombase = SJME_POINTER_TO_JINT(jvm->rom);
	vconfigbase = SJME_POINTER_TO_JINT(jvm->config);
#endif
	
	/* Set initial CPU (the first). */
	cpu = &jvm->threads[0];
	cpu->state = SJME_THREAD_STATE_RUNNING;
	
	/* Set boot pointer to start of ROM. */
	rp = rom;
................................................................................
	}
	
	/* Ignore JAR count and BootJAR index. */
	sjme_memjreadp(jvm, 4, &rp);
	sjme_memjreadp(jvm, 4, &rp);
	
	/* Read and calculate BootJAR position. */
	bootjaroff = sjme_memjreadp(jvm, 4, &rp);
	vbootjarbase = vrombase + bootjaroff;
	rp = bootjar = SJME_POINTER_OFFSET_LONG(rom, bootjaroff);
	
	/* Check JAR magic number. */
	if (sjme_memjreadp(jvm, 4, &rp) != SJME_JAR_MAGIC_NUMBER)
	{
		if (error != NULL)
			*error = SJME_ERROR_INVALIDJARMAGIC;
		
................................................................................
	/* Read boot offset for later. */
	bootoff = sjme_memjreadp(jvm, 4, &rp);
	
	/* Ignore bootsize. */
	sjme_memjreadp(jvm, 4, &rp);
	
	/* Seed initial CPU state. */
	cpu->r[SJME_POOL_REGISTER] =
		vrambase + sjme_memjreadp(jvm, 4, &rp);
	cpu->r[SJME_STATIC_FIELD_REGISTER] =
		vrambase + sjme_memjreadp(jvm, 4, &rp);
	cpu->pc = SJME_JINT_TO_POINTER(vbootjarbase + sjme_memjreadp(jvm, 4, &rp));
	
	/* Bootstrap entry arguments. */
	/* (int __rambase, int __ramsize, int __rombase, int __romsize, */
	/* int __confbase, int __confsize) */
	cpu->r[SJME_ARGBASE_REGISTER + 0] = vrambase;
	cpu->r[SJME_ARGBASE_REGISTER + 1] = ramsize;
	cpu->r[SJME_ARGBASE_REGISTER + 2] = vrombase;
	cpu->r[SJME_ARGBASE_REGISTER + 3] = jvm->romsize;
	cpu->r[SJME_ARGBASE_REGISTER + 4] = vconfigbase;
	cpu->r[SJME_ARGBASE_REGISTER + 5] = jvm->configsize;
	
	/* Address where the BootRAM is read from. */
	rp = SJME_POINTER_OFFSET_LONG(bootjar, bootoff);
	
	/* Copy initial base memory bytes, which is pure big endian. */
	byteram = SJME_JINT_TO_POINTER(vrambase);
	n = sjme_memjreadp(jvm, 4, &rp);
	for (i = 0; i < n; i++)
		sjme_memjwritep(jvm, 1, &byteram, sjme_memjreadp(jvm, 1, &rp));
	
	/* Load all seeds, which restores natural byte order. */
	n = sjme_memjreadp(jvm, 4, &rp);
	for (i = 0; i < n; i++)
	{
		/* Read seed information. */
		seedop = sjme_memjreadp(jvm, 1, &rp);
................................................................................
				*error = SJME_ERROR_INVALIDBOOTRAMSEED;
			
			return 0;
		}
		
		/* Offset value if it is in RAM or JAR ROM. */
		if (seedop == 1)
			seedvalh += vrambase;
		else if (seedop == 2)
			seedvalh += vbootjarbase;
		
		/* Write long value. */
		if (seedsize == 8)
		{
#if defined(SJME_BIG_ENDIAN)
			sjme_memwrite(jvm, 4, ram, seedaddr, seedvalh);
			sjme_memwrite(jvm, 4, ram, seedaddr + SJME_JINT_C(4), seedvall);
................................................................................
{
	sjme_jvmoptions nulloptions;
	void* ram;
	void* rom;
	void* conf;
	sjme_jvm* rv;
	sjme_jint i, l, romsize;
#if defined(SJME_VIRTUAL_MEM)
	sjme_jint vmem = SJME_VIRTUAL_MEM_BASE;
#endif
	
	/* We need native functions. */
	if (nativefuncs == NULL)
		return NULL;
	
	/* Allocate VM state. */
	rv = sjme_malloc(sizeof(*rv));
................................................................................
			*error = SJME_ERROR_NOMEMORY;
		
		sjme_free(rv);
		sjme_free(conf);
		
		return NULL;
	}

#if defined(SJME_VIRTUAL_MEM)
	/* Virtual map config. */
	rv->configbase = vmem;
	vmem = (vmem + SJME_DEFAULT_CONF_SIZE + SJME_VIRTUAL_MEM_MASK) &
		(~SJME_VIRTUAL_MEM_MASK);
#endif
	
	/* If there were no options specified, just use a null set. */
	if (options == NULL)
	{
		memset(&nulloptions, 0, sizeof(nulloptions));
		options = &nulloptions;
	}
................................................................................
			*error = SJME_ERROR_NOMEMORY;
			
		sjme_free(rv);
		sjme_free(conf);
		
		return NULL;
	}
	
#if defined(SJME_VIRTUAL_MEM)
	/* Virtual map RAM. */
	rv->rambase = vmem;
	vmem = (vmem + options->ramsize + SJME_VIRTUAL_MEM_MASK) &
		(~SJME_VIRTUAL_MEM_MASK);
#endif
	
	/* Needed by the VM. */
	rv->ram = ram;
	rv->ramsize = options->ramsize;
	rv->nativefuncs = nativefuncs;
	
	/* Needed by destruction later. */
................................................................................
			/* Offset up. */
			i += l;
		}
		
		/* We copied it, so never make a preset ROM. */
		rv->presetrom = NULL;
	}
	
#if defined(SJME_VIRTUAL_MEM)
	/* Virtual map ROM. */
	rv->rombase = vmem;
	vmem = (vmem + romsize + SJME_VIRTUAL_MEM_MASK) &
		(~SJME_VIRTUAL_MEM_MASK);
#endif
	
	/* Set JVM rom space. */
	rv->rom = rom;
	rv->romsize = romsize;
	
	/* Initialize configuration space. */
	rv->config = conf;
	rv->configsize = SJME_DEFAULT_CONF_SIZE;
	sjme_configinit(conf, SJME_DEFAULT_CONF_SIZE, rv, options, nativefuncs,
		error);
	
	/* Initialize the BootRAM and boot the CPU. */
	if (sjme_initboot(rom, ram, options->ramsize, rv, error) == 0)
	{
		sjme_free(rv);
		sjme_free(ram);
		sjme_free(conf);
................................................................................
		/* If a pre-set ROM is not being used, make sure it gets cleared. */
		if (options->presetrom)
			sjme_free(rom);
		
		return NULL;
	}
	






	/* The JVM is ready to use. */
	return rv;
}

Changes to ratufacoat/sjmerc.h.

34
35
36
37
38
39
40







41
42
43
44
45
46
47
...
170
171
172
173
174
175
176



177
178
179
180




181
182
183
184
185
186
187
		defined(__itanium__) || defined(__powerpc64__) || \
		defined(__ppc64__) || defined(_ARCH_PPC64) || defined(_ARCH_PPC64)
		#define SJME_BITS 64
	#else
		#define SJME_BITS 32
	#endif
#endif








/** Possibly detect endianess. */
#if !defined(SJME_BIG_ENDIAN) && !defined(SJME_LITTLE_ENDIAN)
	/** Defined by the system? */
	#if !defined(SJME_BIG_ENDIAN)
		#if defined(MSB_FIRST) || \
			(defined(WORDS_BIGENDIAN) && WORDS_BIGENDIAN != 0)
................................................................................
/** Unsigned short mask. */
#define SJME_JINT_USHORT_MASK INT32_C(0xFFFF)

/** Pointer conversion. */
#define SJME_POINTER_TO_JINT(x) ((sjme_jint)((uintptr_t)(x)))
#define SJME_JINT_TO_POINTER(x) ((void*)((uintptr_t)((sjme_jint)(x))))




/** Pointer path. */
#define SJME_POINTER_OFFSET(p, o) SJME_JINT_TO_POINTER( \
	SJME_POINTER_TO_JINT(p) + ((sjme_jint)(o)))





/** Standard C format for arguments. */
#define SJME_JVMARG_FORMAT_STDC SJME_JINT_C(1)

/** Open file for reading. */
#define SJME_OPENMODE_READ SJME_JINT_C(1)

/** Open file for read and writing. */







>
>
>
>
>
>
>







 







>
>
>
|



>
>
>
>







34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
...
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
		defined(__itanium__) || defined(__powerpc64__) || \
		defined(__ppc64__) || defined(_ARCH_PPC64) || defined(_ARCH_PPC64)
		#define SJME_BITS 64
	#else
		#define SJME_BITS 32
	#endif
#endif

/** Use virtualized memory? */
#if !defined(SJME_VIRTUAL_MEM)
	#if defined(SJME_BITS) && SJME_BITS == 64
		#define SJME_VIRTUAL_MEM
	#endif
#endif

/** Possibly detect endianess. */
#if !defined(SJME_BIG_ENDIAN) && !defined(SJME_LITTLE_ENDIAN)
	/** Defined by the system? */
	#if !defined(SJME_BIG_ENDIAN)
		#if defined(MSB_FIRST) || \
			(defined(WORDS_BIGENDIAN) && WORDS_BIGENDIAN != 0)
................................................................................
/** Unsigned short mask. */
#define SJME_JINT_USHORT_MASK INT32_C(0xFFFF)

/** Pointer conversion. */
#define SJME_POINTER_TO_JINT(x) ((sjme_jint)((uintptr_t)(x)))
#define SJME_JINT_TO_POINTER(x) ((void*)((uintptr_t)((sjme_jint)(x))))

#define SJME_POINTER_TO_JMEM(x) (((uintptr_t)(x)))
#define SJME_JMEM_TO_POINTER(x) ((void*)((uintptr_t)((x))))

/** Pointer math. */
#define SJME_POINTER_OFFSET(p, o) SJME_JINT_TO_POINTER( \
	SJME_POINTER_TO_JINT(p) + ((sjme_jint)(o)))

/** Pointer math, no precision loss. */
#define SJME_POINTER_OFFSET_LONG(p, o) SJME_JMEM_TO_POINTER( \
	SJME_POINTER_TO_JMEM(p) + ((uintptr_t)(o)))

/** Standard C format for arguments. */
#define SJME_JVMARG_FORMAT_STDC SJME_JINT_C(1)

/** Open file for reading. */
#define SJME_OPENMODE_READ SJME_JINT_C(1)

/** Open file for read and writing. */