Check-in [339bb6b0b3]

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

Overview
Comment:Add support for call traces in SpringCoat.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:339bb6b0b3020a3918e43d18fe92b8dfa1191ea1
User & Date: stephanie.gawroriski 2019-06-17 01:08:09
Context
2019-06-17
07:03
Backup developer notes. check-in: 15cd423edf user: squirreljme tags: trunk
01:08
Add support for call traces in SpringCoat. check-in: 339bb6b0b3 user: stephanie.gawroriski tags: trunk
2019-06-16
23:26
Remove DebugAccess. check-in: e62dad00a3 user: stephanie.gawroriski tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to runt/apis/cldc-compact/cc/squirreljme/jvm/SystemCallError.java.

27
28
29
30
31
32
33




34
35
36
37
38
39
40
	/** The pipe descriptor is not valid. */
	public static final short PIPE_DESCRIPTOR_INVALID =
		-2;
	
	/** Write error when writing to the pipe. */
	public static final short PIPE_DESCRIPTOR_BAD_WRITE =
		-3;




	
	/**
	 * Not used.
	 *
	 * @since 2019/05/23
	 */
	private SystemCallError()







>
>
>
>







27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
	/** The pipe descriptor is not valid. */
	public static final short PIPE_DESCRIPTOR_INVALID =
		-2;
	
	/** Write error when writing to the pipe. */
	public static final short PIPE_DESCRIPTOR_BAD_WRITE =
		-3;
	
	/** Value out of range. */
	public static final short VALUE_OUT_OF_RANGE =
		-4;
	
	/**
	 * Not used.
	 *
	 * @since 2019/05/23
	 */
	private SystemCallError()

Changes to runt/apis/cldc-compact/cc/squirreljme/runtime/cldc/debug/CallTraceElement.java.

586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
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
...
688
689
690
691
692
693
694



695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
			
			throw new todo.TODO();
		}
		
		throw new todo.TODO();
	}
	
	/**
	 * Obtains the current call trace.
	 *
	 * @return The current call trace.
	 * @since 2019/06/16
	 */
	public static final CallTraceElement[] trace()
	{
		return CallTraceElement.traceResolve(CallTraceElement.traceRaw());
	}
	
	/**
	 * Obtains the current raw call trace which has not been resolved.
	 *
	 * @return The raw call trace.
	 * @since 2019/06/16
	 */
	public static final int[] traceRaw()
	{
		// Get the call height, ignore if not supported!
		int callheight = Assembly.sysCallPV(SystemCallIndex.CALL_STACK_HEIGHT);
		if (callheight <= 0 || Assembly.sysCallPV(SystemCallIndex.ERROR_GET,
			SystemCallIndex.CALL_STACK_HEIGHT) != SystemCallError.NO_ERROR)
			return new int[0];



		
		// Get the call parameters
		int[] rv = new int[callheight * CallStackItem.NUM_ITEMS];
		for (int z = 0, base = 0; z < callheight; z++,
			base += CallStackItem.NUM_ITEMS)
			for (int i = 0; i < CallStackItem.NUM_ITEMS; i++)
			{
				// Get parameter
				int vx = Assembly.sysCallPV(SystemCallIndex.CALL_STACK_ITEM,
					z, i);
				
				// Nullify unknown or invalid parameters
				if (Assembly.sysCallPV(SystemCallIndex.ERROR_GET) !=

					SystemCallError.NO_ERROR)
					vx = 0;
				
				// Fill in
				rv[base + i] = vx;
			}
		
................................................................................
			int xsf = Assembly.sysCallV(SystemCallIndex.LOAD_STRING,
				__trace[CallStackItem.SOURCE_FILE]);
			String ssf = ((xsf == 0 || Assembly.sysCallV(
				SystemCallIndex.ERROR_GET, SystemCallIndex.LOAD_STRING) !=
				SystemCallError.NO_ERROR) ?
				(String)null : (String)Assembly.pointerToObject(xsf));
			



			// Build elements
			rv[z] = new CallTraceElement(
				scl,
				smn,
				smt,
				__trace[CallStackItem.PC_ADDRESS],
				ssf,
				__trace[CallStackItem.SOURCE_LINE],
				__trace[CallStackItem.JAVA_OPERATION],
				__trace[CallStackItem.JAVA_PC_ADDRESS]);
		}
		
		// Use the resolved form
		return rv;
	}
}








<
<
<
<
<
<
<
<
<
<
<













>
>
>









|


|
>







 







>
>
>





|











586
587
588
589
590
591
592











593
594
595
596
597
598
599
600
601
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
...
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
			
			throw new todo.TODO();
		}
		
		throw new todo.TODO();
	}
	











	/**
	 * Obtains the current raw call trace which has not been resolved.
	 *
	 * @return The raw call trace.
	 * @since 2019/06/16
	 */
	public static final int[] traceRaw()
	{
		// Get the call height, ignore if not supported!
		int callheight = Assembly.sysCallPV(SystemCallIndex.CALL_STACK_HEIGHT);
		if (callheight <= 0 || Assembly.sysCallPV(SystemCallIndex.ERROR_GET,
			SystemCallIndex.CALL_STACK_HEIGHT) != SystemCallError.NO_ERROR)
			return new int[0];
		
		// Cut call height down to remove this method's frame
		callheight--;
		
		// Get the call parameters
		int[] rv = new int[callheight * CallStackItem.NUM_ITEMS];
		for (int z = 0, base = 0; z < callheight; z++,
			base += CallStackItem.NUM_ITEMS)
			for (int i = 0; i < CallStackItem.NUM_ITEMS; i++)
			{
				// Get parameter
				int vx = Assembly.sysCallPV(SystemCallIndex.CALL_STACK_ITEM,
					1 + z, i);
				
				// Nullify unknown or invalid parameters
				if (Assembly.sysCallPV(SystemCallIndex.ERROR_GET,
					SystemCallIndex.CALL_STACK_ITEM) !=
					SystemCallError.NO_ERROR)
					vx = 0;
				
				// Fill in
				rv[base + i] = vx;
			}
		
................................................................................
			int xsf = Assembly.sysCallV(SystemCallIndex.LOAD_STRING,
				__trace[CallStackItem.SOURCE_FILE]);
			String ssf = ((xsf == 0 || Assembly.sysCallV(
				SystemCallIndex.ERROR_GET, SystemCallIndex.LOAD_STRING) !=
				SystemCallError.NO_ERROR) ?
				(String)null : (String)Assembly.pointerToObject(xsf));
			
			// The PC address
			int pcaddr = __trace[CallStackItem.PC_ADDRESS];
			
			// Build elements
			rv[z] = new CallTraceElement(
				scl,
				smn,
				smt,
				(pcaddr == 0 ? -1 : pcaddr),
				ssf,
				__trace[CallStackItem.SOURCE_LINE],
				__trace[CallStackItem.JAVA_OPERATION],
				__trace[CallStackItem.JAVA_PC_ADDRESS]);
		}
		
		// Use the resolved form
		return rv;
	}
}

Changes to runt/apis/cldc-compact/todo/TODO.java.

434
435
436
437
438
439
440
441

442
443
444
445
446
447
448
	 * @return The call trace element for the current location.
	 * @since 2018/04/02
	 */
	static final CallTraceElement __where()
	{
		// For the SquirrelJME runtime, use the debug stuff to get the
		// current call trace
		CallTraceElement[] stack = CallTraceElement.trace();

		
		// Get the first one which is not in this class
		for (CallTraceElement e : stack)
		{
			String cn = e.className();
			if (cn == null)
				cn = "<unknown>";







|
>







434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
	 * @return The call trace element for the current location.
	 * @since 2018/04/02
	 */
	static final CallTraceElement __where()
	{
		// For the SquirrelJME runtime, use the debug stuff to get the
		// current call trace
		CallTraceElement[] stack = CallTraceElement.traceResolve(
			CallTraceElement.traceRaw());
		
		// Get the first one which is not in this class
		for (CallTraceElement e : stack)
		{
			String cn = e.className();
			if (cn == null)
				cn = "<unknown>";

Changes to runt/klib/supervisor/cc/squirreljme/jvm/SystemCallError.java.

27
28
29
30
31
32
33




34
35
36
37
38
39
40
	/** The pipe descriptor is not valid. */
	public static final short PIPE_DESCRIPTOR_INVALID =
		-2;
	
	/** Write error when writing to the pipe. */
	public static final short PIPE_DESCRIPTOR_BAD_WRITE =
		-3;




	
	/**
	 * Not used.
	 *
	 * @since 2019/05/23
	 */
	private SystemCallError()







>
>
>
>







27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
	/** The pipe descriptor is not valid. */
	public static final short PIPE_DESCRIPTOR_INVALID =
		-2;
	
	/** Write error when writing to the pipe. */
	public static final short PIPE_DESCRIPTOR_BAD_WRITE =
		-3;
	
	/** Value out of range. */
	public static final short VALUE_OUT_OF_RANGE =
		-4;
	
	/**
	 * Not used.
	 *
	 * @since 2019/05/23
	 */
	private SystemCallError()

Changes to runt/libs/springcoat-vm/cc/squirreljme/vm/springcoat/SpringMachine.java.

109
110
111
112
113
114
115




116
117
118
119
120
121
122
	
	/** String to long map. */
	private final Map<String, Long> _strstringtolong =
		new HashMap<>();
	
	/** System properties. */
	final Map<String, String> _sysproperties;




	
	/** The next thread ID to use. */
	private volatile int _nextthreadid;
	
	/** The next long to choose. */
	private long _strnextlong;
	







>
>
>
>







109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
	
	/** String to long map. */
	private final Map<String, Long> _strstringtolong =
		new HashMap<>();
	
	/** System properties. */
	final Map<String, String> _sysproperties;
	
	/** Unique objects by integer. */
	final Map<Integer, SpringObject> _uniquebyint =
		new HashMap<>();
	
	/** The next thread ID to use. */
	private volatile int _nextthreadid;
	
	/** The next long to choose. */
	private long _strnextlong;
	

Changes to runt/libs/springcoat-vm/cc/squirreljme/vm/springcoat/SpringThreadWorker.java.

6
7
8
9
10
11
12

13
14
15
16
17
18
19
20
21
22
23
....
1146
1147
1148
1149
1150
1151
1152












1153
1154
1155
1156
1157
1158
1159
....
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
....
4151
4152
4153
4154
4155
4156
4157



4158
4159
4160
4161
4162
4163
4164
4165
4166
4167

4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180


























































































4181
4182
4183
4184
4185
4186
4187
....
4215
4216
4217
4218
4219
4220
4221
4222






































4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
....
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
....
4386
4387
4388
4389
4390
4391
4392

















































































4393
4394
4395
4396
4397
4398
4399
// ---------------------------------------------------------------------------
// SquirrelJME is under the GNU General Public License v3+, or later.
// See license.mkd for licensing and copyright information.
// ---------------------------------------------------------------------------

package cc.squirreljme.vm.springcoat;


import cc.squirreljme.jvm.SystemCallError;
import cc.squirreljme.jvm.SystemCallIndex;
import cc.squirreljme.runtime.cldc.asm.ConsoleOutput;
import cc.squirreljme.runtime.cldc.asm.DebugAccess;
import cc.squirreljme.runtime.cldc.asm.SystemAccess;
import cc.squirreljme.runtime.cldc.asm.SystemProperties;
import cc.squirreljme.runtime.cldc.lang.ApiLevel;
import cc.squirreljme.runtime.cldc.lang.GuestDepth;
import cc.squirreljme.runtime.cldc.lang.OperatingSystemType;
import cc.squirreljme.vm.VMClassLibrary;
import java.io.IOException;
................................................................................
				return Float.intBitsToFloat((Integer)__args[0]);
				
				// Convert long bits to double
			case "cc/squirreljme/jvm/Assembly::" +
				"longBitsToDouble:(J)D":
				return Double.longBitsToDouble((Long)__args[0]);
				












				// System calls (no return value)
			case "cc/squirreljme/jvm/Assembly::" +
				"sysCall:(S)V":
			case "cc/squirreljme/jvm/Assembly::" +
				"sysCall:(SI)V":
			case "cc/squirreljme/jvm/Assembly::" +
				"sysCall:(SII)V":
................................................................................
							(Integer)__args[3]);
						return 0;
					}
					else
						return ConsoleOutput.ERROR_INVALIDFD;
				}
				
				// Fatal report of a raw call trace in early TODO code
			case "cc/squirreljme/runtime/cldc/asm/DebugAccess::" +
				"fatalTodoReport:([I)V":
				{
					// Format text to the output
					StringBuilder sb = new StringBuilder("[");
					
					// Print hex codes
					SpringArrayObject hex = (SpringArrayObject)__args[0];
					for (int i = 0, n = hex.length(); i < n; i++)
					{
						if (i > 0)
							sb.append(", ");
						sb.append(String.format("%08x",
							hex.<Integer>get(Integer.class, i)));
					}
					
					// End
					sb.append("]");
					
					// {@squirreljme.error BK1h Virtual machine code executed
					// a fatal TODOs report indicating unimplemented code,
					// failing. (The hex codes for the TODOs trace)}
					throw new SpringVirtualMachineException(
						String.format("BK1h %s", sb));
				}
			
				// Return the call trace
			case "cc/squirreljme/runtime/cldc/asm/DebugAccess::" +
				"rawCallTrace:()[I":
				{
					// Need to get all the stack frames first
					SpringThread.Frame[] frames = this.thread.frames();
					int numframes = frames.length;
					
					// Setup return value which stores all the frame data
					SpringMachine machine = this.machine;
					int[] rv = new int[DebugAccess.TRACE_COUNT * numframes];
					for (int i = numframes - 1, o = 0; i >= 0; i--,
						o += DebugAccess.TRACE_COUNT)
					{
						SpringThread.Frame frame = frames[i];
						
						SpringMethod inmethod = frame.method();
						int pc = frame.lastExecutedPc();
						
						// Only if the method is valid
						if (inmethod != null)
						{
							// Class hilo
							SpringMachine.longToInt(o + 0, rv,
								machine.debugUnresolveString(
									inmethod.inClass().toString()));
							
							// Method hilo
							SpringMachine.longToInt(o + 2, rv,
								machine.debugUnresolveString(
									inmethod.nameAndType().name().toString()));
							
							// Descriptor hilo
							SpringMachine.longToInt(o + 4, rv,
								machine.debugUnresolveString(
									inmethod.nameAndType().type().toString()));
							
							// File string
							SpringMachine.longToInt(o + 8, rv,
								machine.debugUnresolveString(
									inmethod.inFile()));
						}
						
						// Not valid
						else
						{
							SpringMachine.longToInt(o + 0, rv, -1);
							SpringMachine.longToInt(o + 2, rv, -1);
							SpringMachine.longToInt(o + 4, rv, -1);
							SpringMachine.longToInt(o + 8, rv, -1);
						}
						
						// Program counter hilo
						rv[o + 6] = 0;
						rv[o + 7] = frame.lastExecutedPc();
						
						// Line of code being executed
						rv[o + 10] = frame.lastExecutedPcSourceLine();
					}
					
					return rv;
				}
				
				// Resolve string pointer
			case "cc/squirreljme/runtime/cldc/asm/DebugAccess::" +
				"resolveString:(J)Ljava/lang/String;":
				return this.asVMObject(
					this.machine.debugResolveString((Long)__args[0]));
					
				// Unresolve string pointer
			case "cc/squirreljme/runtime/cldc/asm/DebugAccess::" +
				"unresolveString:(Ljava/lang/String;)J":
				return this.machine.debugUnresolveString(
					this.<String>asNativeObject(String.class, __args[0]));
				
				// Accelerated graphics
			case "cc/squirreljme/runtime/cldc/asm/NativeDisplayAccess::" +
				"accelGfx:(I)Z":
				return this.machine.nativedisplay.accelGfx(
					(Integer)__args[0]);
				
				// Accelerated graphics operation
................................................................................
		{
				// Check if system call is supported
			case SystemCallIndex.QUERY_INDEX:
				{
					err = 0;
					switch (__args[0])
					{



						case SystemCallIndex.ERROR_GET:
						case SystemCallIndex.ERROR_SET:
						case SystemCallIndex.TIME_LO_MILLI_WALL:
						case SystemCallIndex.TIME_HI_MILLI_WALL:
						case SystemCallIndex.TIME_LO_NANO_MONO:
						case SystemCallIndex.TIME_HI_NANO_MONO:
						case SystemCallIndex.VMI_MEM_FREE:
						case SystemCallIndex.VMI_MEM_USED:
						case SystemCallIndex.VMI_MEM_MAX:
						case SystemCallIndex.GARBAGE_COLLECT:

						case SystemCallIndex.EXIT:
						case SystemCallIndex.API_LEVEL:
						case SystemCallIndex.PD_OF_STDIN:
						case SystemCallIndex.PD_OF_STDOUT:
						case SystemCallIndex.PD_OF_STDERR:
						case SystemCallIndex.PD_WRITE_BYTE:
							rv = 1;
							break;
						
						default:
							rv = 0;
							break;
					}


























































































				}
				break;
				
				// Get error
			case SystemCallIndex.ERROR_GET:
				{
					// If the ID is valid then a bad array access will be used
................................................................................
						errors[dx] = __args[0];
					}
					
					// Always succeeds
					err = 0;
				}
				break;
			






































			// Current wall clock milliseconds (low).
			case SystemCallIndex.TIME_LO_MILLI_WALL:
				{
					rv = (int)(System.currentTimeMillis());
					err = 0;
				}
				break;

			// Current wall clock milliseconds (high).
			case SystemCallIndex.TIME_HI_MILLI_WALL:
				{
					rv = (int)(System.currentTimeMillis() >>> 32);
					err = 0;
				}
				break;

			// Current monotonic clock nanoseconds (low).
			case SystemCallIndex.TIME_LO_NANO_MONO:
				{
					rv = (int)(System.nanoTime());
					err = 0;
				}
				break;

			// Current monotonic clock nanoseconds (high).
			case SystemCallIndex.TIME_HI_NANO_MONO:
				{
					rv = (int)(System.nanoTime() >>> 32);
					err = 0;
				}
				break;
			
................................................................................
				break;
			
				// VM information: Memory max bytes
			case SystemCallIndex.VMI_MEM_MAX:
				{
					rv = (int)Math.min(Integer.MAX_VALUE,
						Runtime.getRuntime().maxMemory());
					err = 0;
				}
				break;
				
				// Invoke the garbage collector
			case SystemCallIndex.GARBAGE_COLLECT:
				{
					Runtime.getRuntime().gc();
					
					rv = 0;
					err = 0;
				}
				break;
				
				// Exit the VM
			case SystemCallIndex.EXIT:
				{
					// Tell everything to cleanup and exit
					this.thread.profiler.exitAll(System.nanoTime());
					this.machine.exit((Integer)__args[0]);
					
					rv = 0;
					err = 0;
				}
				break;
				
				// API level
			case SystemCallIndex.API_LEVEL:
				{
................................................................................
		{
			errors[__si] = err;
		}
		
		// Use returning value
		return rv;
	}

















































































	
	/**
	 * Performs a special invoke.
	 *
	 * @param __i The instruction.
	 * @param __t The current thread.
	 * @param __f The current frame.







>



<







 







>
>
>
>
>
>
>
>
>
>
>
>







 







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







 







>
>
>


|
|
|
|
|
|
|
|
>
|
|
|
|
|
|







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|







|







|







|







 







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







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







6
7
8
9
10
11
12
13
14
15
16

17
18
19
20
21
22
23
....
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
....
1326
1327
1328
1329
1330
1331
1332






































































































1333
1334
1335
1336
1337
1338
1339
....
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
....
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
....
4313
4314
4315
4316
4317
4318
4319






















4320
4321
4322
4323
4324
4325
4326
....
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
// ---------------------------------------------------------------------------
// SquirrelJME is under the GNU General Public License v3+, or later.
// See license.mkd for licensing and copyright information.
// ---------------------------------------------------------------------------

package cc.squirreljme.vm.springcoat;

import cc.squirreljme.jvm.CallStackItem;
import cc.squirreljme.jvm.SystemCallError;
import cc.squirreljme.jvm.SystemCallIndex;
import cc.squirreljme.runtime.cldc.asm.ConsoleOutput;

import cc.squirreljme.runtime.cldc.asm.SystemAccess;
import cc.squirreljme.runtime.cldc.asm.SystemProperties;
import cc.squirreljme.runtime.cldc.lang.ApiLevel;
import cc.squirreljme.runtime.cldc.lang.GuestDepth;
import cc.squirreljme.runtime.cldc.lang.OperatingSystemType;
import cc.squirreljme.vm.VMClassLibrary;
import java.io.IOException;
................................................................................
				return Float.intBitsToFloat((Integer)__args[0]);
				
				// Convert long bits to double
			case "cc/squirreljme/jvm/Assembly::" +
				"longBitsToDouble:(J)D":
				return Double.longBitsToDouble((Long)__args[0]);
				
				// Object to pointer
			case "cc/squirreljme/jvm/Assembly::" +
				"objectToPointer:(Ljava/lang/Object;)I":
			case "cc/squirreljme/jvm/Assembly::" +
				"objectToPointerRefQueue:(Ljava/lang/Object;)I":
				return this.uniqueObjectToPointer((SpringObject)__args[0]);
				
				// Pointer to object
			case "cc/squirreljme/jvm/Assembly::" +
				"pointerToObject:(I)Ljava/lang/Object;":
				return this.uniquePointerToObject((Integer)__args[0]);
				
				// System calls (no return value)
			case "cc/squirreljme/jvm/Assembly::" +
				"sysCall:(S)V":
			case "cc/squirreljme/jvm/Assembly::" +
				"sysCall:(SI)V":
			case "cc/squirreljme/jvm/Assembly::" +
				"sysCall:(SII)V":
................................................................................
							(Integer)__args[3]);
						return 0;
					}
					else
						return ConsoleOutput.ERROR_INVALIDFD;
				}
				






































































































				// Accelerated graphics
			case "cc/squirreljme/runtime/cldc/asm/NativeDisplayAccess::" +
				"accelGfx:(I)Z":
				return this.machine.nativedisplay.accelGfx(
					(Integer)__args[0]);
				
				// Accelerated graphics operation
................................................................................
		{
				// Check if system call is supported
			case SystemCallIndex.QUERY_INDEX:
				{
					err = 0;
					switch (__args[0])
					{
						case SystemCallIndex.API_LEVEL:
						case SystemCallIndex.CALL_STACK_HEIGHT:
						case SystemCallIndex.CALL_STACK_ITEM:
						case SystemCallIndex.ERROR_GET:
						case SystemCallIndex.ERROR_SET:
						case SystemCallIndex.EXIT:
						case SystemCallIndex.FATAL_TODO:
						case SystemCallIndex.GARBAGE_COLLECT:
						case SystemCallIndex.LOAD_STRING:
						case SystemCallIndex.PD_OF_STDERR:
						case SystemCallIndex.PD_OF_STDIN:
						case SystemCallIndex.PD_OF_STDOUT:
						case SystemCallIndex.PD_WRITE_BYTE:
						case SystemCallIndex.TIME_HI_MILLI_WALL:
						case SystemCallIndex.TIME_HI_NANO_MONO:
						case SystemCallIndex.TIME_LO_MILLI_WALL:
						case SystemCallIndex.TIME_LO_NANO_MONO:
						case SystemCallIndex.VMI_MEM_FREE:
						case SystemCallIndex.VMI_MEM_MAX:
						case SystemCallIndex.VMI_MEM_USED:
							rv = 1;
							break;
						
						default:
							rv = 0;
							break;
					}
				}
				break;
				
				// Returns the height of the call stack
			case SystemCallIndex.CALL_STACK_HEIGHT:
				{
					rv = this.thread.frames().length;
					err = 0;
				}
				break;
				
				// Returns the given call stack item
			case SystemCallIndex.CALL_STACK_ITEM:
				{
					// Need to get all the stack frames first
					SpringThread.Frame[] frames = this.thread.frames();
					int numframes = frames.length;
					int curf = (numframes - 1) - __args[0];
					
					// Out of range item
					if (curf < 0 || curf >= numframes)
					{
						rv = -1;
						err = SystemCallError.VALUE_OUT_OF_RANGE;
					}
					
					// Depends on the item
					else
					{
						// Reset
						rv = err = 0;
						
						// Get method we are in
						SpringMethod inmethod = frames[curf].method();
						
						// Depends on the item
						switch (__args[1])
						{
								// Class name
							case CallStackItem.CLASS_NAME:
								if (inmethod == null)
									err = SystemCallError.VALUE_OUT_OF_RANGE;
								else
									rv = this.uniqueStringId(
										inmethod.inClass().toString());
								break;

								// The method name.
							case CallStackItem.METHOD_NAME:
								if (inmethod == null)
									err = SystemCallError.VALUE_OUT_OF_RANGE;
								else
									rv = this.uniqueStringId(inmethod.
										nameAndType().name().toString());
								break;

								// The method type.
							case CallStackItem.METHOD_TYPE:
								if (inmethod == null)
									err = SystemCallError.VALUE_OUT_OF_RANGE;
								else
									rv = this.uniqueStringId(inmethod.
										nameAndType().type().toString());
								break;

								// The current file.
							case CallStackItem.SOURCE_FILE:
								if (inmethod == null)
									err = SystemCallError.VALUE_OUT_OF_RANGE;
								else
									rv = this.uniqueStringId(
										inmethod.inFile());
								break;

								// Source line.
							case CallStackItem.SOURCE_LINE:
								rv = frames[curf].lastExecutedPcSourceLine();
								break;

								// The PC address.
							case CallStackItem.PC_ADDRESS:
							case CallStackItem.JAVA_PC_ADDRESS:
								rv = frames[curf].lastExecutedPc();
								break;

							default:
								err = SystemCallError.VALUE_OUT_OF_RANGE;
								break;
						}
					}
				}
				break;
				
				// Get error
			case SystemCallIndex.ERROR_GET:
				{
					// If the ID is valid then a bad array access will be used
................................................................................
						errors[dx] = __args[0];
					}
					
					// Always succeeds
					err = 0;
				}
				break;
				
				// Exit the VM
			case SystemCallIndex.EXIT:
				{
					// Tell everything to cleanup and exit
					this.thread.profiler.exitAll(System.nanoTime());
					this.machine.exit((Integer)__args[0]);
					
					rv = 0;
					err = 0;
				}
				break;
				
				// Fatal ToDo
			case SystemCallIndex.FATAL_TODO:
				// {@squirreljme.error BK1h Virtual machine code executed
				// a fatal Todo.}
				rv = err = 0;
				throw new SpringVirtualMachineException("BK1h");
				
				// Invoke the garbage collector
			case SystemCallIndex.GARBAGE_COLLECT:
				{
					Runtime.getRuntime().gc();
					
					rv = 0;
					err = 0;
				}
				break;
				
				// Loads a string
			case SystemCallIndex.LOAD_STRING:
				{
					rv = this.uniqueObjectToPointer((SpringObject)
						this.asVMObject(this.uniqueString(__args[0])));
					err = 0;
				}
				break;
			
				// Current wall clock milliseconds (low).
			case SystemCallIndex.TIME_LO_MILLI_WALL:
				{
					rv = (int)(System.currentTimeMillis());
					err = 0;
				}
				break;

				// Current wall clock milliseconds (high).
			case SystemCallIndex.TIME_HI_MILLI_WALL:
				{
					rv = (int)(System.currentTimeMillis() >>> 32);
					err = 0;
				}
				break;

				// Current monotonic clock nanoseconds (low).
			case SystemCallIndex.TIME_LO_NANO_MONO:
				{
					rv = (int)(System.nanoTime());
					err = 0;
				}
				break;

				// Current monotonic clock nanoseconds (high).
			case SystemCallIndex.TIME_HI_NANO_MONO:
				{
					rv = (int)(System.nanoTime() >>> 32);
					err = 0;
				}
				break;
			
................................................................................
				break;
			
				// VM information: Memory max bytes
			case SystemCallIndex.VMI_MEM_MAX:
				{
					rv = (int)Math.min(Integer.MAX_VALUE,
						Runtime.getRuntime().maxMemory());






















					err = 0;
				}
				break;
				
				// API level
			case SystemCallIndex.API_LEVEL:
				{
................................................................................
		{
			errors[__si] = err;
		}
		
		// Use returning value
		return rv;
	}
	
	/**
	 * Converts an object to a unique pointer.
	 *
	 * @param __p The object to convert.
	 * @return The resulting pointer.
	 * @since 2019/06/16
	 */
	public final int uniqueObjectToPointer(SpringObject __p)
	{
		// Null reference?
		if (__p == SpringNullObject.NULL)
			return 0;
		
		// Lock
		Map<Integer, SpringObject> ubi = this.machine._uniquebyint;
		synchronized (ubi)
		{
			// See if it already has been mapped?
			for (Map.Entry<Integer, SpringObject> e : ubi.entrySet())
				if (e.getValue() == __p)
					return e.getKey();
			
			// Otherwise add it
			int rv = ubi.size();
			ubi.put(rv, __p);
			return rv;
		}
	}
	
	/**
	 * Converts an object to a unique pointer.
	 *
	 * @param __p The object to convert.
	 * @return The resulting pointer.
	 * @since 2019/06/16
	 */
	public final SpringObject uniquePointerToObject(int __p)
	{
		// Null reference?
		if (__p == 0)
			return SpringNullObject.NULL;
		
		// Find mapped object already
		Map<Integer, SpringObject> ubi = this.machine._uniquebyint;
		synchronized (ubi)
		{
			SpringObject rv = ubi.get(__p);
			if (rv == null)
				return SpringNullObject.NULL;
			return rv;
		}
	}
	
	/**
	 * Returns the string of the given ID.
	 *
	 * @param __id The ID to get.
	 * @return The resulting string.
	 * @since 2019/06/16
	 */
	public final String uniqueString(int __id)
	{
		return this.machine.debugResolveString((int)__id);
	}
	
	/**
	 * Returns a unique ID for the given string.
	 *
	 * @param __s The String to get the ID od.
	 * @throws NullPointerException On null arguments.
	 * @since 2019/06/16
	 */
	public final int uniqueStringId(String __s)
		throws NullPointerException
	{
		if (__s == null)
			throw new NullPointerException("NARG");
		
		return (int)this.machine.debugUnresolveString(__s);
	}
	
	/**
	 * Performs a special invoke.
	 *
	 * @param __i The instruction.
	 * @param __t The current thread.
	 * @param __f The current frame.