<commit_before>import {getAddressOffset, isTextAddress, isStackAddress} from './address';
import {TEXT_MASK, STACK_MASK} from './address';
import SIGSEGV from './error/sigsegv';

interface SegmentLength {
  textLength: number;
  stackLength: number;
}

export default class MemoryManager {
  private text: Uint32Array;
  private stack: Uint32Array;

  constructor(sl: SegmentLength) {
    this.text = new Uint32Array(sl.textLength >> 2);
    this.stack = new Uint32Array(sl.stackLength >> 2);
  }

  getTextBaseAddr(): number {
    return TEXT_MASK;
  }

  getTextTopAddr(): number {
    return TEXT_MASK + this.text.length;
  }

  getStackBaseAddr(): number {
    return STACK_MASK;
  }

  getStackTopAddr(): number {
    return STACK_MASK + this.stack.length;
  }

  readWord(addr: number): number {
    const offset = getAddressOffset(addr);
    if (isTextAddress(addr)) {
      if (offset >= this.text.length) {
        throw new SIGSEGV('text segment address out of bounds');
      }
      return this.text[offset];
    } else if (isStackAddress(addr)) {
      if (offset >= this.stack.length) {
        throw new SIGSEGV('stack segment address out of bounds');
      }
      return this.stack[offset];
    }
    throw new SIGSEGV('bad address');
  }

  writeWord(addr: number, value: number): void {
    const offset = getAddressOffset(addr);
    if (isTextAddress(addr)) {
      if (offset >= this.text.length) {
        throw new SIGSEGV('text segment address out of bounds');
      }
      this.text[offset] = value;
    } else if (isStackAddress(addr)) {
      if (offset >= this.stack.length) {
        throw new SIGSEGV('stack segment address out of bounds');
      }
      this.stack[offset] = value;
    } else {
      throw new SIGSEGV('bad address');
    }
  }
}

<commit_msg>top address calculation fix and alignment checks<commit_after>import {getAddressOffset, isTextAddress, isStackAddress} from './address';
import {TEXT_MASK, STACK_MASK} from './address';
import SIGSEGV from './error/sigsegv';

interface SegmentLength {
  textLength: number;
  stackLength: number;
}

export default class MemoryManager {
  private text: Uint32Array;
  private stack: Uint32Array;

  constructor(sl: SegmentLength) {
    this.text = new Uint32Array(sl.textLength >> 2);
    this.stack = new Uint32Array(sl.stackLength >> 2);
  }

  getTextBaseAddr(): number {
    return TEXT_MASK;
  }

  getTextTopAddr(): number {
    return TEXT_MASK | (this.text.length << 2);
  }

  getStackBaseAddr(): number {
    return STACK_MASK;
  }

  getStackTopAddr(): number {
    return STACK_MASK | (this.stack.length << 2);
  }

  readWord(addr: number): number {
    const offset = getAddressOffset(addr);
    if ((offset & 0x3) != 0) {
      throw new SIGSEGV('malaligned address');
    }
    offset >>= 2;
    if (isTextAddress(addr)) {
      if (offset >= this.text.length) {
        throw new SIGSEGV('text segment address out of bounds');
      }
      return this.text[offset];
    } else if (isStackAddress(addr)) {
      if (offset >= this.stack.length) {
        throw new SIGSEGV('stack segment address out of bounds');
      }
      return this.stack[offset];
    }
    throw new SIGSEGV('bad address');
  }

  writeWord(addr: number, value: number): void {
    const offset = getAddressOffset(addr);
    if ((offset & 0x3) != 0) {
      throw new SIGSEGV('malaligned address');
    }
    offset >>= 2;
    if (isTextAddress(addr)) {
      if (offset >= this.text.length) {
        throw new SIGSEGV('text segment address out of bounds');
      }
      this.text[offset] = value;
    } else if (isStackAddress(addr)) {
      if (offset >= this.stack.length) {
        throw new SIGSEGV('stack segment address out of bounds');
      }
      this.stack[offset] = value;
    } else {
      throw new SIGSEGV('bad address');
    }
  }
}


{'fraction_non_alphanumeric': 0.12457649205108158, 'fraction_numerical': 0.007818608287724784, 'mean_word_length': 3.002085505735141, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, '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': '2169372', 'n_tokens_mistral': 1237, 'n_tokens_neox': 1181, 'n_words': 316}