Skip to content

Commit

Permalink
fixed array bugs; wrote demo program
Browse files Browse the repository at this point in the history
  • Loading branch information
nmosier committed Jan 16, 2020
1 parent 528acc9 commit ad7519d
Show file tree
Hide file tree
Showing 14 changed files with 794 additions and 74 deletions.
17 changes: 17 additions & 0 deletions DIARY
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,26 @@ structs on [1], [2], [3] share the same unique ID, while [4] has a different uni
[1 hr] Wrote tests for loop; fixed binary operation bug (invalid 'case' fallthrough in switch statement, leading to incorrect type being assigned). Added comma operator (the one that separates
expressions).

01/16/2020
[1 hr] Wrote script that searches through the TI 84+ CE C runtime (CRT) for given symbols (e.g. sprintf, imulu, etc.) and then prints out the disassembled contents. This script was tricky because the CRT functions use a trampoline (for stability across releases) -- that is, the static address `__sprintf' points to a single jump instruction that jumps to the real implemenation of _sprintf.

[1 hr] I wrote a new C runtime function 'cdivu' that divides two unsigned chars. (The CRT already on the calculator did not have this, and fast division is non-trivial). I unit-tested this, and it works.

[2 hr] Wrote program 'memcpy.c' to demonstrate current state of compiler. It contains three functions:
```
int main(int argc, char **argv); /* main */
void *mymemset(void *dst, char c, size_t bytes); /* re-implementation of C memset function */
void *mymemcpy(void *dst, void *src, size_t bytes); /* re-implementation of C memcpy function */
```
At first, the program wouldn't compile; later, it wouldn't produce correct output and/or crash. I went back and fixed those bugs. This program behave correctly now.
memcpy.c compiles to approximately 170 lines of code (!!!). This should be a very easy to target for optimization.

TODO
[ ] typedef
[X] ++, --
[ ] +=, *=, etc.
[ ] for


need bdivs, bdivu
add signed/unsigned
145 changes: 143 additions & 2 deletions asm/asm-crt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace zc::z80 {

CRT crt;

CRT::CRT(const std::initializer_list<std::string>& l) {
for (const std::string& str : l) {
Label *label = new Label(str);
Expand Down Expand Up @@ -38,5 +36,148 @@ namespace zc::z80 {
}
}

CRT g_crt {"__longjmp",
"__memchr",
"__memcmp",
"__memcpy",
"__memmove",
"__memset",
"__memclear",
"_printf",
"__setjmp",
"_sprintf",
"__strcat",
"__strchr",
"__strcmp",
"__strcpy",
"__strcspn",
"__strlen",
"__strncat",
"__strncmp",
"__strncpy",
"__strpbrk",
"__strrchr",
"__strspn",
"__strstr",
"_strtok",
"_ret",
"__bldiy",
"__bshl",
"__bshru",
"__bstiy",
"__bstix",
"__case",
"__case16",
"__case16D",
"__case24",
"__case24D",
"__case8",
"__case8D",
"__frameset",
"__frameset0",
"__iand",
"__icmpzero",
"__idivs",
"__idivu",
"__idvrmu",
"__ildix",
"__ildiy",
"__imul_b",
"__imulu",
"__imuls",
"__indcall",
"__ineg",
"__inot",
"__ior",
"__irems",
"__iremu",
"__ishl",
"__ishl_b",
"__ishrs",
"__ishrs_b",
"__ishru",
"__ishru_b",
"__istix",
"__istiy",
"__itol",
"__ixor",
"__ladd",
"__ladd_b",
"__land",
"__lcmps",
"__lcmpu",
"__lcmpzero",
"__ldivs",
"__ldivu",
"__ldvrmu",
"__lldix",
"__lldiy",
"__lmuls",
"__lmulu",
"__lneg",
"__lnot",
"__lor",
"__lrems",
"__lremu",
"__lshl",
"__lshrs",
"__lshru",
"__lstix",
"__lstiy",
"__lsub",
"__lxor",
"__sand",
"__scmpzero",
"__sdivs",
"__sdivu",
"__seqcase",
"__seqcaseD",
"__setflag",
"__sldix",
"__sldiy",
"__smuls",
"__smulu",
"__sneg",
"__snot",
"__sor",
"__srems",
"__sremu",
"__sshl",
"__sshl_b",
"__sshrs",
"__sshrs_b",
"__sshru",
"__sshru_b",
"__sstix",
"__sstiy",
"__stoi",
"__stoiu",
"__sxor",
"__fppack",
"__fadd",
"__fcmp",
"__fdiv",
"__ftol",
"__ultof",
"__ltof",
"__fmul",
"__fneg",
"__fsub",
"_FLTMAX",
"_sqrtf",
"__frbtof",
"__frftob",
"__frftoub",
"__frftoi",
"__frftoui",
"__frftos",
"__frftous",
"__fritof",
"__fruitof",
"__frstof",
"__frubtof",
"__frustof",
"__cdivu",
};

}
2 changes: 1 addition & 1 deletion asm/asm-crt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace zc::z80 {
const Pair& add(const std::string& name);
};

extern CRT crt;
extern CRT g_crt;
}

#endif
7 changes: 6 additions & 1 deletion ast/ast-type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ namespace zc {
void Enscope(SemantEnv& env);

virtual ASTType *Address() = 0;
virtual ASTType *Dereference(SemantEnv *env = nullptr) = 0;
virtual ASTType *Dereference(SemantEnv *env);
virtual bool decays() const { return false; }
virtual ASTType *Decay() { return this; }

virtual int bytes() const = 0;
void FrameGen(StackFrame& frame) const;
Expand All @@ -79,6 +81,7 @@ namespace zc {
virtual ASTType *get_containee() const override { return pointee(); }
int depth() const { return depth_; }
ASTType *pointee() const { return pointee_; }
bool is_void() const { return pointee()->kind() == Kind::TYPE_VOID; }

template <typename... Args>
static PointerType *Create(Args... args) {
Expand Down Expand Up @@ -544,6 +547,8 @@ namespace zc {

virtual ASTType *Address() override;
virtual ASTType *Dereference(SemantEnv *env) override { return elem(); }
virtual bool decays() const override { return true; }
virtual ASTType *Decay() override;

virtual int bytes() const override;

Expand Down
3 changes: 0 additions & 3 deletions c.ypp
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,6 @@ decl: decl_specs optional_init_declarator_list ';' {
declarator->get_declarables($$);
zc::Declaration *decl = $1->GetDecl(zc::g_semant_error, declarator);
enscope_typedef(decl);
// auto type = zc::ASTType::Create(specs_type, declarator);
// $$->push_back(zc::VarDeclaration::Create(declarator->sym(), false,
// type, @1));
$$->push_back(decl);
}

Expand Down
Loading

0 comments on commit ad7519d

Please sign in to comment.