Skip to content

Commit

Permalink
risc-v
Browse files Browse the repository at this point in the history
  • Loading branch information
vuphuc24601 committed Apr 21, 2022
0 parents commit 1d6f24d
Show file tree
Hide file tree
Showing 22 changed files with 636 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
################################################################################
# Makefile for the CS 3410 RISC-V Interpreter Project #
################################################################################

# Additional flags for the compiler
CFLAGS := -std=c99 -D_BSD_SOURCE -Wall -g

# Default target to run, which creates a `riscv_interpreter` executable
all: riscv_interpreter

# Compiles the student linkedlist.c into an object file
# Then, combines the object file into a single `linkedlist` executable
linkedlist: linkedlist.o linkedlist_main.o
gcc $(CFLAGS) -o $@ $^

# Compiles the student linkedlist.c, hashtable.c into object files
# Then, combines the object files into a single `hashtable` executable
hashtable: linkedlist.o hashtable.o hashtable_main.o
gcc $(CFLAGS) -o $@ $^

# Compiles the student linkedlist.c, hashtable.c, and riscv.c into object files
# Then, combines the object files into a single `riscv_interpreter` executable
riscv_interpreter: linkedlist.o hashtable.o riscv.o riscv_interpreter.o
gcc $(CFLAGS) -Werror -o $@ $^

# Wildcard rule that allows for the compilation of a *.c file to a *.o file
%.o : %.c
gcc -c $(CFLAGS) $< -o $@

# Removes any executables and compiled object files
clean:
rm -f linkedlist hashtable riscv_interpreter *.o
Binary file added hashtable
Binary file not shown.
56 changes: 56 additions & 0 deletions hashtable.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"
#include "hashtable.h"

struct hashtable {
// TODO: define hashtable struct to use linkedlist as buckets
int capacity;
int num_buckets;
int size;
linkedlist_t **buckets;
};

/**
* Hash function to hash a key into the range [0, max_range)
*/
static int hash(int key, int max_range) {
// TODO: feel free to write your own hash function (NOT REQUIRED)
key = (key > 0) ? key : -key;
return key % max_range;
}

hashtable_t *ht_init(int num_buckets) {
// TODO: create a new hashtable
hashtable_t *table = malloc(sizeof(hashtable_t));
table->num_buckets = num_buckets;
table->size = 0;
table->buckets = malloc(num_buckets * sizeof(linkedlist_t*));
for (int i=0; i<num_buckets; i++) {
table->buckets[i] = ll_init();
}
return table;
}

void ht_add(hashtable_t *table, int key, int value) {
// TODO: create a new mapping from key -> value.
// If the key already exists, replace the value.
int index = hash(key, table->num_buckets);
linkedlist_t *list = table->buckets[index];

table->size -= ll_size(list);
ll_add(table->buckets[index], key, value);
table->size += ll_size(list);
}

int ht_get(hashtable_t *table, int key) {
// TODO: retrieve the value mapped to the given key.
// If it does not exist, return 0
int index = hash(key, table->num_buckets);
return ll_get(table->buckets[index], key);
}

int ht_size(hashtable_t *table) {
// TODO: return the number of mappings in this hashtable
return table->size;
}
33 changes: 33 additions & 0 deletions hashtable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Type alias for the internal representation of hashtable.
* Defined in hashtable.c:
*
* struct hashtable {
* ...
* }
*
* And later reference this struct type using hashtable_t.
*/
typedef struct hashtable hashtable_t;

/**
* Return a pointer to a new hashtable that has been initialized with a fixed
* number of buckets
*/
hashtable_t *ht_init(int num_buckets);

/**
* Add a mapping from key->value to the hashtable.
*/
void ht_add(hashtable_t *table, int key, int value);

/**
* Retrieves the value associated with the given key from the hashtable.
* If the key does not exist in the hashtable, return 0.
*/
int ht_get(hashtable_t *table, int key);

/**
* Returns the number of unique key->value mappings in the hashtable.
*/
int ht_size(hashtable_t *table);
Binary file added hashtable.o
Binary file not shown.
17 changes: 17 additions & 0 deletions hashtable_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdio.h>
#include "hashtable.h"

int main() {
hashtable_t *table = ht_init(16);
printf("Adding mapping from 10 -> 123\n");
ht_add(table, 10, 123);
printf("Get 10 -> %d (expected 123)\n", ht_get(table, 10));
ht_add(table, 10, 256);
printf("Adding mapping from 10 -> 256\n");
printf("Get 10 -> %d (expected 256)\n", ht_get(table, 10));
printf("Size = %d (expected 1)\n", ht_size(table));
printf("Adding mapping from 20 -> 9\n");
ht_add(table, 20, 9);
printf("Get 20 -> %d (expected 9)\n", ht_get(table, 20));
printf("Size = %d (expected 2)\n", ht_size(table));
}
Binary file added hashtable_main.exe
Binary file not shown.
Binary file added hashtable_main.o
Binary file not shown.
Binary file added linkedlist
Binary file not shown.
98 changes: 98 additions & 0 deletions linkedlist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <stdio.h>
#include <stdlib.h>
#include "linkedlist.h"

struct linkedlist
{
struct linkedlist_node *first;
// TODO: define linked list metadata
int size;
};

struct linkedlist_node
{
// TODO: define the linked list node
int key;
int value;
linkedlist_node_t *next;
};
typedef struct linkedlist_node linkedlist_node_t;

linkedlist_t *ll_init()
{
// TODO: create a new linked list

// We have done this TODO for you as an example of how to use malloc().
// You might want to read more about malloc() from Linux Manual page.
// Usually free() should be used together with malloc(). For example,
// the linkedlist you are currently implementing usually have free() in the
// ll_delete() function. Since we are not asking you to implement
// the ll_delete() function, you will not be using free() in this case.

// First, you use C's sizeof function to determine
// the size of the linkedlist_t data type in bytes.
// Then, you use malloc to set aside that amount of space in memory.
// malloc returns a void pointer to the memory you just allocated, but
// we can assign this pointer to the specific type we know we created
linkedlist_t *list = malloc(sizeof(linkedlist_t));

// TODO: set metadata for your new list and return the new list
list->first = NULL;
list->size = 0;

return list;
}

void ll_add(linkedlist_t *list, int key, int value)
{
// TODO: create a new node and add to the front of the linked list if a
// node with the key does not already exist.
// Otherwise, replace the existing value with the new value.
linkedlist_node_t *current = list->first;
linkedlist_node_t *new_node = malloc(sizeof(linkedlist_node_t));
new_node->key = key;
new_node->value = value;
new_node->next = NULL;

if (current == NULL) {
list->first = new_node;
list->size += 1;
} else {
while (current->next != NULL) {
if (current->key == key) {
current->value = value;
return;
} else {
current = current->next;
}
}
if (current->key == key) {
current->value = value;
return;
}
current->next = new_node;
list->size += 1;
}
}

int ll_get(linkedlist_t *list, int key)
{
// TODO: go through each node in the linked list and return the value of
// the node with a matching key.
// If it does not exist, return 0.
linkedlist_node_t *current = list->first;
while (current != NULL) {
if (current->key == key) {
return current->value;
} else {
current = current->next;
}
}
return 0;
}

int ll_size(linkedlist_t *list)
{
// TODO: return the number of nodes in this linked list
return list->size;
}
34 changes: 34 additions & 0 deletions linkedlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Type alias for the internal representation of linkedlist.
* Defined in linkedlist.c:
*
* struct linkedlist {
* ...
* }
*
* And later reference this struct type using linkedlist_t.
*/
typedef struct linkedlist linkedlist_t;
typedef struct linkedlist_node linkedlist_node_t;

/**
* Return a pointer to a new linkedlist
*/
linkedlist_t *ll_init();

/**
* Add a mapping from key->value to the associative linkedlist.
* If a mapping with the given key already exists, replace the value.
*/
void ll_add(linkedlist_t *list, int key, int value);

/**
* Retrieves the value associated with the given key from the linkedlist.
* If the key does not exist in the linkedlist, return 0.
*/
int ll_get(linkedlist_t *list, int key);

/**
* Returns the number of unique key->value mappings in the linkedlist.
*/
int ll_size(linkedlist_t *list);
Binary file added linkedlist.o
Binary file not shown.
17 changes: 17 additions & 0 deletions linkedlist_main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdio.h>
#include "linkedlist.h"

int main() {
linkedlist_t *list = ll_init();
printf("Adding mapping from 10 -> 123\n");
ll_add(list, 10, 123);
printf("Get 10 -> %d (expected 123)\n", ll_get(list, 10));
ll_add(list, 10, 256);
printf("Adding mapping from 10 -> 256\n");
printf("Get 10 -> %d (expected 256)\n", ll_get(list, 10));
printf("Size = %d (expected 1)\n", ll_size(list));
printf("Adding mapping from 20 -> 9\n");
ll_add(list, 20, 9);
printf("Get 20 -> %d (expected 9)\n", ll_get(list, 20));
printf("Size = %d (expected 2)\n", ll_size(list));
}
Binary file added linkedlist_main.exe
Binary file not shown.
Binary file added linkedlist_main.o
Binary file not shown.
Loading

0 comments on commit 1d6f24d

Please sign in to comment.