<commit_before>
#
# Register 0-1 are reserved as memory address pointers
#   s0, v0 - pointer to base of shared region
#   s1, v1 - pointer to base of private region
#

from random import randint
import math

NUM_INSTRUCTIONS = 128

class Generator:
	def __init__(self):
		pass

	# Note: this will endian swap the data
	def writeWord(self, instr):
		self.file.write('%02x%02x%02x%02x\n' % ((instr & 0xff), ((instr >> 8) & 0xff), ((instr >> 16) & 0xff), ((instr >> 24) & 0xff)))

	def generate(self, path):
		self.file = open(path, 'w')

		# NOTE: number of instructions is hard-coded into this code chunk.  Must
		# reassemble it if that changes
		prolog = [
			0x3c003c40, # s0 = 0xf
			0x8c00005e, # cr30 = s0      ; Enable all strands

			# Load interesting values into scratchpad registers
			0x3c3f4c60, # s3 = 4051
			0x1c3f4463, # s3 = s3 * 4049
			0x1c00ec83, # s4 = s3 * 59
			0x1c019ca4, # s5 = s4 * 103
			0xc18204c5, # s6 = s5 ^ s4
			0x1c0044e6, # s7 = s6 * 17
			0x2000063,	# v3 = s3
			0x2000084,	# v4 = s4
			0x20000a5,	# v5 = s5
			0x20000c6,	# v6 = s6
			0x20000e7,	# v7 = s7
			0xc1d21c63,	# v3{s7} = v3 ^ v4
			0xc1d29884,	# v4{s6} = v4 ^ v5
			0xc1d314a5,	# v5{s5} = v5 ^ v6
			0xc1d390c6,	# v6{s4} = v6 ^ v7
			0xc1d18ce7,	# v7{s3} = v7 ^ v3

			# Set up memory areas and branch
			0xac000040, # s2 = cr0       ; Get strand ID
			0x14000422, # s1 = s2 + 1	
			0x2c004421, # s1 = s1 << 17  ; Multiply by 128k, so each strand starts on a new page
			0x02000021, # v1 = s1        ; set up vector register as the same
			0x2c002442, # s2 = s2 << 9   ; Multiply strand by 512 bytes (128 instructions)
			0xc28107ff  # pc = pc + s2	 ; jump to start address for this strand
		]

		epilog = [
			0x00000000,	# Because random jumps can be generated above,
			0x00000000, # We need to pad with 8 NOPs to ensure
			0x00000000, # The last instruction doesn't jump over our
			0x00000000, # Cleanup code.
			0x00000000,
			0x00000000,
			0x00000000,
			0x00000000,
			0x00000000,
			0x00000000,
			0x00000000,
			0x00000000,
			0xf7ffff80  # done goto done
		]
		
		for word in prolog:
			self.writeWord(word)		
		
		for strand in range(4):
			for x in range(NUM_INSTRUCTIONS - len(epilog)):
				self.writeWord(self.nextInstruction())

			# stop the strand	
			for word in epilog:
				self.writeWord(word)		
		
		self.file.close()

	# Only allocate 8 registers so we are more likely to have dependencies
	def randomRegister(self):
		return randint(2, 10)

	def nextInstruction(self):
		instructionType = randint(0, 100)
		if instructionType < 30:		# 30% chance of format A
			# format A (register arithmetic)
			dest = self.randomRegister()
			src1 = self.randomRegister()
			src2 = self.randomRegister()
			mask = self.randomRegister()
			fmt = randint(0, 6)
			opcode = randint(0, 0x19)	# for now, no floating point
			while True:
				opcode = randint(0, 0x19)	
				if opcode == 8:
					continue	# Don't allow division (could generate div by zero)
					
				if opcode == 13 and (opcode != 4 and opcode != 5 and opcode != 6):
					continue	# Shuffle can only be used with vector/vector forms
					
				break		
			
<commit_msg>Randomly initialize exclusive memory areas for each strand.<commit_after>
#
# Register 0-1 are reserved as memory address pointers
#   s0, v0 - pointer to base of shared region
#   s1, v1 - pointer to base of private region
#

from random import randint
import math, sys

NUM_INSTRUCTIONS = 128

class Generator:
	def __init__(self):
		pass

	# Note: this will endian swap the data
	def writeWord(self, instr):
		self.file.write('%02x%02x%02x%02x\n' % ((instr & 0xff), ((instr >> 8) & 0xff), ((instr >> 16) & 0xff), ((instr >> 24) & 0xff)))

	def generate(self, path):
		self.file = open(path, 'w')

		# NOTE: number of instructions is hard-coded into this code chunk.  Must
		# reassemble it if that changes
		prolog = [
			0x3c003c40, # s0 = 0xf
			0x8c00005e, # cr30 = s0      ; Enable all strands

			# Load interesting values into scratchpad registers
			0x3c3f4c60, # s3 = 4051
			0x1c3f4463, # s3 = s3 * 4049
			0x1c00ec83, # s4 = s3 * 59
			0x1c019ca4, # s5 = s4 * 103
			0xc18204c5, # s6 = s5 ^ s4
			0x1c0044e6, # s7 = s6 * 17
			0x2000063,	# v3 = s3
			0x2000084,	# v4 = s4
			0x20000a5,	# v5 = s5
			0x20000c6,	# v6 = s6
			0x20000e7,	# v7 = s7
			0xc1d21c63,	# v3{s7} = v3 ^ v4
			0xc1d29884,	# v4{s6} = v4 ^ v5
			0xc1d314a5,	# v5{s5} = v5 ^ v6
			0xc1d390c6,	# v6{s4} = v6 ^ v7
			0xc1d18ce7,	# v7{s3} = v7 ^ v3

			# Set up memory areas and branch
			0xac000040, # s2 = cr0       ; Get strand ID
			0x14000422, # s1 = s2 + 1	
			0x2c004421, # s1 = s1 << 17  ; Multiply by 128k, so each strand starts on a new page
			0x02000021, # v1 = s1        ; set up vector register as the same
			0x2c002442, # s2 = s2 << 9   ; Multiply strand by 512 bytes (128 instructions)
			0xc28107ff  # pc = pc + s2	 ; jump to start address for this strand
		]

		epilog = [
			0x00000000,	# Because random jumps can be generated above,
			0x00000000, # We need to pad with 8 NOPs to ensure
			0x00000000, # The last instruction doesn't jump over our
			0x00000000, # Cleanup code.
			0x00000000,
			0x00000000,
			0x00000000,
			0x00000000,
			0x00000000,
			0x00000000,
			0x00000000,
			0x00000000,
			0xf7ffff80  # done goto done
		]
		
		for word in prolog:
			self.writeWord(word)		
		
		for strand in range(4):
			for x in range(NUM_INSTRUCTIONS - len(epilog)):
				self.writeWord(self.nextInstruction())

			# stop the strand	
			for word in epilog:
				self.writeWord(word)		
		
		for x in range(128 * 1024):
			self.writeWord(randint(0, sys.maxint))
		
		self.file.close()

	# Only allocate 8 registers so we are more likely to have dependencies
	def randomRegister(self):
		return randint(2, 10)

	def nextInstruction(self):
		instructionType = randint(0, 100)
		if instructionType < 30:		# 30% chance of format A
			# format A (register arithmetic)
			dest = self.randomRegister()
			src1 = self.randomRegister()
			src2 = self.randomRegister()
			mask = self.randomRegister()
			fmt = randint(0, 6)
			opcode = randint(0, 0x19)	# for now, no floating point
			while True:
				opcode = randint(0, 0x19)	
				if opcode == 8:
					continue	# Don't allow division (could generate div by zero)
					
				if opcode == 13 and (opcode != 4 and opcode != 5 and opcode != 6):
					continue	# Shuffle can only be used with vector/vector forms
					
				break		
			

{'fraction_non_alphanumeric': 0.10050015629884339, 'fraction_numerical': 0.13347921225382933, 'mean_word_length': 2.827153110047847, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 24, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '943023', 'n_tokens_mistral': 3284, 'n_tokens_neox': 2444, 'n_words': 769}