Skip to content

Commit

Permalink
debug hashtable free
Browse files Browse the repository at this point in the history
  • Loading branch information
yangminz committed Apr 18, 2021
1 parent febae2c commit 316c38b
Show file tree
Hide file tree
Showing 15 changed files with 220 additions and 106 deletions.
32 changes: 16 additions & 16 deletions cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,10 @@ def build(key):
"./src/common/print.c",
"./src/common/convert.c",
"./src/common/cleanup.c",
"./src/datastruct/trie.c",
"./src/datastruct/array.c",
"./src/algorithm/trie.c",
"./src/algorithm/array.c",
"./src/hardware/cpu/isa.c",
"./src/hardware/cpu/mmu.c",
"./src/hardware/memory/dram.c",
"-o", EXE_BIN_MACHINE
]
],
Expand All @@ -161,9 +160,9 @@ def build(key):
"./src/common/convert.c",
"./src/common/tagmalloc.c",
"./src/common/cleanup.c",
"./src/datastruct/array.c",
"./src/datastruct/hashtable.c",
"./src/datastruct/linkedlist.c",
"./src/algorithm/array.c",
"./src/algorithm/hashtable.c",
"./src/algorithm/linkedlist.c",
"./src/linker/parseElf.c",
"./src/linker/staticlink.c",
"-o", EXE_BIN_LINKER
Expand All @@ -177,12 +176,12 @@ def build(key):
"./src/common/convert.c",
"./src/common/tagmalloc.c",
"./src/common/cleanup.c",
"./src/datastruct/array.c",
"./src/datastruct/hashtable.c",
"./src/datastruct/linkedlist.c",
"./src/algorithm/array.c",
"./src/algorithm/hashtable.c",
"./src/algorithm/linkedlist.c",
"./src/linker/parseElf.c",
"./src/linker/staticlink.c",
"-o", "./bin/staticlink.so"
"-o", "./bin/staticLinker.so"
],
[
"/usr/bin/gcc-7",
Expand All @@ -192,9 +191,9 @@ def build(key):
"./src/common/convert.c",
"./src/common/tagmalloc.c",
"./src/common/cleanup.c",
"./src/datastruct/array.c",
"./src/datastruct/hashtable.c",
"./src/datastruct/linkedlist.c",
"./src/algorithm/array.c",
"./src/algorithm/hashtable.c",
"./src/algorithm/linkedlist.c",
"./src/linker/linker.c",
"-ldl", "-o", "./bin/link"
],
Expand All @@ -210,13 +209,14 @@ def build(key):
def run(key):
assert(os.path.isdir("./bin/"))
bin_map = {
KEY_MACHINE : EXE_BIN_MACHINE,
KEY_LINKER : EXE_BIN_LINKER
KEY_MACHINE : [EXE_BIN_MACHINE],
KEY_LINKER : [EXE_BIN_LINKER],
"dll" : ["./bin/link", "main", "sum", "-o", "output"],
}
if not key in bin_map:
print("input the correct binary key:", bin_map.keys())
exit()
subprocess.run([bin_map[key]])
subprocess.run(bin_map[key])

def debug(key):
assert(os.path.isdir("./bin/"))
Expand Down
44 changes: 44 additions & 0 deletions files/exe/output.eof.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
44
3
.text,0x400000,5,32
.data,0x400800,37,3
.symtab,0x0,40,4
push %rbp
mov %rsp,%rbp
sub $0x10,%rsp
mov $0x2,%esi
lea 0x0000000000000948(%rip),%rdi
callq 0x0000000000000160
mov %rax,-0x8(%rbp)
mov -0x8(%rbp),%rax
leaveq
retq
push %rbp
mov %rsp,%rbp
mov %rdi,-0x18(%rbp)
mov %rsi,-0x20(%rbp)
movq $0x0,-0x8(%rbp)
movq $0x0,-0x10(%rbp)
jmp 3d
mov -0x10(%rbp),%rax
lea 0x0(,%rax,8),%rdx
mov -0x18(%rbp),%rax
add %rdx,%rax
mov (%rax),%rax
add %rax,-0x8(%rbp)
addq $0x1,-0x10(%rbp)
mov -0x10(%rbp),%rax
cmp -0x20(%rbp),%rax
jb 1e
mov 0x0000000000000170(%rip),%rdx
mov -0x8(%rbp),%rax
add %rdx,%rax
pop %rbp
retq
0x0000000012340000
0x000000000000abcd
0x0000000f00000000
main,STB_GLOBAL,STT_FUNC,.text,0,10
sum,STB_GLOBAL,STT_FUNC,.text,10,22
array,STB_GLOBAL,STT_OBJECT,.data,0,2
bias,STB_GLOBAL,STT_OBJECT,.data,2,1
2 changes: 1 addition & 1 deletion src/datastruct/array.c → src/algorithm/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <stdlib.h>
#include <stdint.h>
#include "headers/common.h"
#include "headers/datastruct.h"
#include "headers/algorithm.h"

array_t *array_construct(int size)
{
Expand Down
7 changes: 5 additions & 2 deletions src/datastruct/hashtable.c → src/algorithm/hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <string.h>
#include <stdint.h>
#include "headers/common.h"
#include "headers/datastruct.h"
#include "headers/algorithm.h"

static uint64_t hash_function(char *str)
{
Expand Down Expand Up @@ -71,6 +71,9 @@ void hashtable_free(hashtable_t *tab)
return;
}

debug_printf(DEBUG_DATASTRUCTURE, "free hashtable:\n");
print_hashtable(tab);

for (int i = 0; i < tab->num; ++ i)
{
hashtable_bucket_t *b = tab->directory[i];
Expand All @@ -79,7 +82,7 @@ void hashtable_free(hashtable_t *tab)
continue;
}

for (int j = 0; j < tab->size; ++ j)
for (int j = 0; j < b->counter; ++ j)
{
if (b->karray != NULL && b->karray[j] != NULL)
{
Expand Down
2 changes: 1 addition & 1 deletion src/datastruct/linkedlist.c → src/algorithm/linkedlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "headers/datastruct.h"
#include "headers/algorithm.h"

// constructor and destructor
linkedlist_t *linkedlist_construct()
Expand Down
2 changes: 1 addition & 1 deletion src/datastruct/trie.c → src/algorithm/trie.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "headers/cpu.h"
#include "headers/memory.h"
#include "headers/common.h"
#include "headers/datastruct.h"
#include "headers/algorithm.h"

static int get_index(char c)
{
Expand Down
2 changes: 1 addition & 1 deletion src/common/cleanup.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "headers/common.h"
#include "headers/datastruct.h"
#include "headers/algorithm.h"

typedef void (*cleanup_t)();

Expand Down
2 changes: 1 addition & 1 deletion src/common/tagmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include <string.h>
#include <stdio.h>
#include "headers/common.h"
#include "headers/datastruct.h"
#include "headers/algorithm.h"

static uint64_t compute_tag(char *str);
static void tag_destroy();
Expand Down
60 changes: 3 additions & 57 deletions src/hardware/cpu/isa.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,63 +14,8 @@
#include "headers/cpu.h"
#include "headers/memory.h"
#include "headers/common.h"
#include "headers/datastruct.h"

/*======================================*/
/* instruction set architecture */
/*======================================*/

// data structures
typedef enum INST_OPERATOR
{
INST_MOV, // 0
INST_PUSH, // 1
INST_POP, // 2
INST_LEAVE, // 3
INST_CALL, // 4
INST_RET, // 5
INST_ADD, // 6
INST_SUB, // 7
INST_CMP, // 8
INST_JNE, // 9
INST_JMP, // 10
} op_t;

typedef enum OPERAND_TYPE
{
EMPTY, // 0
IMM, // 1
REG, // 2
MEM_IMM, // 3
MEM_REG1, // 4
MEM_IMM_REG1, // 5
MEM_REG1_REG2, // 6
MEM_IMM_REG1_REG2, // 7
MEM_REG2_SCAL, // 8
MEM_IMM_REG2_SCAL, // 9
MEM_REG1_REG2_SCAL, // 10
MEM_IMM_REG1_REG2_SCAL // 11
} od_type_t;

typedef struct OPERAND_STRUCT
{
od_type_t type; // IMM, REG, MEM
uint64_t imm; // immediate number
uint64_t scal; // scale number to register 2
uint64_t reg1; // main register
uint64_t reg2; // register 2
} od_t;

// local variables are allocated in stack in run-time
// we don't consider local STATIC variables
// ref: Computer Systems: A Programmer's Perspective 3rd
// Chapter 7 Linking: 7.5 Symbols and Symbol Tables
typedef struct INST_STRUCT
{
op_t op; // enum of operators. e.g. mov, call, etc.
od_t src; // operand src of instruction
od_t dst; // operand dst of instruction
} inst_t;
#include "headers/algorithm.h"
#include "headers/instruction.h"

/*======================================*/
/* parse assembly instruction */
Expand Down Expand Up @@ -668,6 +613,7 @@ static void call_handler(od_t *src_od, od_t *dst_od)
va2pa(cpu_reg.rsp),
cpu_pc.rip + sizeof(char) * MAX_INSTRUCTION_CHAR);
// jump to target function address
// TODO: support PC relative addressing
cpu_pc.rip = src;
cpu_flags.__flags_value = 0;
}
Expand Down
1 change: 0 additions & 1 deletion src/headers/datastruct.h → src/headers/algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

#include <stdint.h>


/*======================================*/
/* Circular Doubly Linked List */
/*======================================*/
Expand Down
74 changes: 74 additions & 0 deletions src/headers/instruction.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/* BCST - Introduction to Computer Systems
* Author: [email protected]
* Github: https://github.com/yangminz/bcst_csapp
* Bilibili: https://space.bilibili.com/4564101
* Zhihu: https://www.zhihu.com/people/zhao-yang-min
* This project (code repository and videos) is exclusively owned by yangminz
* and shall not be used for commercial and profitting purpose
* without yangminz's permission.
*/

// include guards to prevent double declaration of any identifiers
// such as types, enums and static variables
#ifndef INSTRUCTION_GUARD
#define INSTRUCTION_GUARD

#include <stdint.h>

/*======================================*/
/* instruction set architecture */
/*======================================*/

// data structures
typedef enum INST_OPERATOR
{
INST_MOV, // 0
INST_PUSH, // 1
INST_POP, // 2
INST_LEAVE, // 3
INST_CALL, // 4
INST_RET, // 5
INST_ADD, // 6
INST_SUB, // 7
INST_CMP, // 8
INST_JNE, // 9
INST_JMP, // 10
} op_t;

typedef enum OPERAND_TYPE
{
EMPTY, // 0
IMM, // 1
REG, // 2
MEM_IMM, // 3
MEM_REG1, // 4
MEM_IMM_REG1, // 5
MEM_REG1_REG2, // 6
MEM_IMM_REG1_REG2, // 7
MEM_REG2_SCAL, // 8
MEM_IMM_REG2_SCAL, // 9
MEM_REG1_REG2_SCAL, // 10
MEM_IMM_REG1_REG2_SCAL // 11
} od_type_t;

typedef struct OPERAND_STRUCT
{
od_type_t type; // IMM, REG, MEM
uint64_t imm; // immediate number
uint64_t scal; // scale number to register 2
uint64_t reg1; // main register
uint64_t reg2; // register 2
} od_t;

// local variables are allocated in stack in run-time
// we don't consider local STATIC variables
// ref: Computer Systems: A Programmer's Perspective 3rd
// Chapter 7 Linking: 7.5 Symbols and Symbol Tables
typedef struct INST_STRUCT
{
op_t op; // enum of operators. e.g. mov, call, etc.
od_t src; // operand src of instruction
od_t dst; // operand dst of instruction
} inst_t;

#endif
3 changes: 2 additions & 1 deletion src/headers/linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

#include <stdint.h>
#include <stdlib.h>
#include "headers/datastruct.h"
#include "headers/algorithm.h"

#define MAX_CHAR_SECTION_NAME (32)

Expand Down Expand Up @@ -104,5 +104,6 @@ typedef struct
void parse_elf(char *filename, elf_t *elf);
void free_elf(elf_t *elf);
void link_elf(elf_t **srcs, int num_srcs, elf_t *dst);
void write_eof(const char *filename, elf_t *eof);

#endif
Loading

0 comments on commit 316c38b

Please sign in to comment.