comparison gen/toir.cpp @ 203:e881c9b1c738 trunk

[svn r219] Fixed: the tango/lib/gc/basic garbage collector now compiles and links into an executable (change in tango/lib/llvmdc-posix.mak), closes #5 . Changed: removed the crappy realloc based dynamic memory runtime and started moving over to DMD style runtime support, part of moving to real GC. Fixed: dynamic arrays now use GC runtime for allocating memory. Fixed: new expression now use GC for allocating memory. Changed: revamped the dynamic array support routines related to dynamic memory. Fixed: assertions no longer create exsessive allocas. Changed: misc. minor cleanups.
author lindquist
date Tue, 13 May 2008 14:42:09 +0200
parents 8f9191180c7a
children 9d44ec83acd1
comparison
equal deleted inserted replaced
202:56e0c5b1d428 203:e881c9b1c738
570 Logger::println("performing assignment"); 570 Logger::println("performing assignment");
571 571
572 DImValue* im = r->isIm(); 572 DImValue* im = r->isIm();
573 if (!im || !im->inPlace()) { 573 if (!im || !im->inPlace()) {
574 Logger::println("assignment not inplace"); 574 Logger::println("assignment not inplace");
575 if (l->isArrayLen()) 575 if (DArrayLenValue* al = l->isArrayLen())
576 DtoResizeDynArray(l->getLVal(), r->getRVal()); 576 {
577 DSliceValue* slice = DtoResizeDynArray(l->getType(), l, r);
578 DtoAssign(l, slice);
579 }
577 else 580 else
581 {
578 DtoAssign(l, r); 582 DtoAssign(l, r);
583 }
579 } 584 }
580 585
581 if (l->isSlice() || l->isComplex()) 586 if (l->isSlice() || l->isComplex())
582 return l; 587 return l;
583 588
1930 assert(newtype); 1935 assert(newtype);
1931 assert(!allocator && "custom allocators not yet supported"); 1936 assert(!allocator && "custom allocators not yet supported");
1932 1937
1933 Type* ntype = DtoDType(newtype); 1938 Type* ntype = DtoDType(newtype);
1934 1939
1940 // new class
1935 if (ntype->ty == Tclass) { 1941 if (ntype->ty == Tclass) {
1936 Logger::println("new class"); 1942 Logger::println("new class");
1937 return DtoNewClass((TypeClass*)ntype, this); 1943 return DtoNewClass((TypeClass*)ntype, this);
1938 } 1944 }
1945 // new dynamic array
1946 else if (ntype->ty == Tarray)
1947 {
1948 Logger::println("new dynamic array: %s", newtype->toChars());
1949 // get dim
1950 assert(arguments);
1951 assert(arguments->dim == 1);
1952 DValue* sz = ((Expression*)arguments->data[0])->toElem(p);
1953 // allocate & init
1954 return DtoNewDynArray(newtype, sz, true);
1955 }
1956 // new static array
1957 else if (ntype->ty == Tsarray)
1958 {
1959 assert(0);
1960 }
1961 // new struct
1962 else if (ntype->ty == Tstruct)
1963 {
1964 // allocate
1965 llvm::Value* mem = DtoNew(newtype);
1966 // init
1967 TypeStruct* ts = (TypeStruct*)ntype;
1968 if (ts->isZeroInit()) {
1969 DtoStructZeroInit(mem);
1970 }
1971 else {
1972 assert(ts->sym);
1973 DtoStructCopy(mem,ts->sym->ir.irStruct->init);
1974 }
1975 return new DImValue(type, mem, false);
1976 }
1977 // new basic type
1978 else
1979 {
1980 // allocate
1981 llvm::Value* mem = DtoNew(newtype);
1982 // BUG: default initialize
1983 // return
1984 return new DImValue(type, mem, false);
1985 }
1986
1987 /*
1939 1988
1940 const llvm::Type* t = DtoType(ntype); 1989 const llvm::Type* t = DtoType(ntype);
1941 1990
1942 llvm::Value* emem = 0; 1991 llvm::Value* emem = 0;
1943 bool inplace = false; 1992 bool inplace = false;
1967 } 2016 }
1968 else { 2017 else {
1969 assert(0 && "num args to 'new' != 1"); 2018 assert(0 && "num args to 'new' != 1");
1970 } 2019 }
1971 } 2020 }
2021 // simple new of a single non-class, non-array value
1972 else { 2022 else {
1973 emem = new llvm::MallocInst(t,"tmp",p->scopebb()); 2023 // get runtime function
2024 llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_allocmemoryT");
2025 // get type info
2026 llvm::Constant* ti = DtoTypeInfoOf(newtype);
2027 assert(isaPointer(ti));
2028 // call runtime
2029 llvm::SmallVector<llvm::Value*,1> arg;
2030 arg.push_back(ti);
2031 emem = p->ir->CreateCall(fn, arg.begin(), arg.end(), ".gc_mem");
1974 } 2032 }
1975 2033
1976 if (ntype->ty == Tstruct) { 2034 if (ntype->ty == Tstruct) {
1977 TypeStruct* ts = (TypeStruct*)ntype; 2035 TypeStruct* ts = (TypeStruct*)ntype;
1978 if (ts->isZeroInit()) { 2036 if (ts->isZeroInit()) {
1983 DtoStructCopy(emem,ts->sym->ir.irStruct->init); 2041 DtoStructCopy(emem,ts->sym->ir.irStruct->init);
1984 } 2042 }
1985 } 2043 }
1986 2044
1987 return new DImValue(type, emem, inplace); 2045 return new DImValue(type, emem, inplace);
2046 */
2047 assert(0);
1988 } 2048 }
1989 2049
1990 ////////////////////////////////////////////////////////////////////////////////////////// 2050 //////////////////////////////////////////////////////////////////////////////////////////
1991 2051
1992 DValue* DeleteExp::toElem(IRState* p) 2052 DValue* DeleteExp::toElem(IRState* p)
2051 { 2111 {
2052 Logger::print("ArrayLengthExp::toElem: %s | %s\n", toChars(), type->toChars()); 2112 Logger::print("ArrayLengthExp::toElem: %s | %s\n", toChars(), type->toChars());
2053 LOG_SCOPE; 2113 LOG_SCOPE;
2054 2114
2055 DValue* u = e1->toElem(p); 2115 DValue* u = e1->toElem(p);
2116 Logger::println("e1 = %s", e1->type->toChars());
2056 2117
2057 if (p->topexp() && p->topexp()->e1 == this) 2118 if (p->topexp() && p->topexp()->e1 == this)
2058 { 2119 {
2059 return new DArrayLenValue(type, u->getLVal()); 2120 return new DArrayLenValue(e1->type, u->getLVal());
2060 } 2121 }
2061 else 2122 else
2062 { 2123 {
2063 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false); 2124 llvm::Value* zero = llvm::ConstantInt::get(llvm::Type::Int32Ty, 0, false);
2064 llvm::Value* ptr = DtoGEP(u->getRVal(),zero,zero,"tmp",p->scopebb()); 2125 llvm::Value* ptr = DtoGEP(u->getRVal(),zero,zero,"tmp",p->scopebb());
2486 2547
2487 Type* t = DtoDType(type); 2548 Type* t = DtoDType(type);
2488 2549
2489 bool arrNarr = DtoDType(e1->type) == DtoDType(e2->type); 2550 bool arrNarr = DtoDType(e1->type) == DtoDType(e2->type);
2490 2551
2552 // array ~ array
2553 if (arrNarr)
2554 {
2555 return DtoCatArrays(type, e1, e2);
2556 }
2557 // array ~ element
2558 // element ~ array
2559 else
2560 {
2561 return DtoCatArrayElement(type, e1, e2);
2562 }
2563
2564 /*
2491 IRExp* ex = p->topexp(); 2565 IRExp* ex = p->topexp();
2492 if (ex && ex->e2 == this) { 2566 if (ex && ex->e2 == this) {
2493 assert(ex->v); 2567 assert(ex->v);
2494 if (arrNarr) 2568 if (arrNarr)
2495 DtoCatArrays(ex->v->getLVal(),e1,e2); 2569 DtoCatArrays(ex->v->getLVal(),e1,e2);
2500 else { 2574 else {
2501 assert(t->ty == Tarray); 2575 assert(t->ty == Tarray);
2502 const llvm::Type* arrty = DtoType(t); 2576 const llvm::Type* arrty = DtoType(t);
2503 llvm::Value* dst = new llvm::AllocaInst(arrty, "tmpmem", p->topallocapoint()); 2577 llvm::Value* dst = new llvm::AllocaInst(arrty, "tmpmem", p->topallocapoint());
2504 if (arrNarr) 2578 if (arrNarr)
2579 DtoCatAr
2505 DtoCatArrays(dst,e1,e2); 2580 DtoCatArrays(dst,e1,e2);
2506 else 2581 else
2507 DtoCatArrayElement(dst,e1,e2); 2582 DtoCatArrayElement(dst,e1,e2);
2508 return new DVarValue(type, dst, true); 2583 return new DVarValue(type, dst, true);
2509 } 2584 }
2585 */
2510 } 2586 }
2511 2587
2512 ////////////////////////////////////////////////////////////////////////////////////////// 2588 //////////////////////////////////////////////////////////////////////////////////////////
2513 2589
2514 DValue* CatAssignExp::toElem(IRState* p) 2590 DValue* CatAssignExp::toElem(IRState* p)
2521 Type* e1type = DtoDType(e1->type); 2597 Type* e1type = DtoDType(e1->type);
2522 Type* elemtype = DtoDType(e1type->next); 2598 Type* elemtype = DtoDType(e1type->next);
2523 Type* e2type = DtoDType(e2->type); 2599 Type* e2type = DtoDType(e2->type);
2524 2600
2525 if (e2type == elemtype) { 2601 if (e2type == elemtype) {
2526 DtoCatAssignElement(l->getLVal(),e2); 2602 DSliceValue* slice = DtoCatAssignElement(l,e2);
2603 DtoAssign(l, slice);
2527 } 2604 }
2528 else if (e1type == e2type) { 2605 else if (e1type == e2type) {
2529 DtoCatAssignArray(l->getLVal(),e2); 2606 DSliceValue* slice = DtoCatAssignArray(l,e2);
2607 DtoAssign(l, slice);
2530 } 2608 }
2531 else 2609 else
2532 assert(0 && "only one element at a time right now"); 2610 assert(0 && "only one element at a time right now");
2533 2611
2534 return 0; 2612 return l;
2535 } 2613 }
2536 2614
2537 ////////////////////////////////////////////////////////////////////////////////////////// 2615 //////////////////////////////////////////////////////////////////////////////////////////
2538 2616
2539 DValue* FuncExp::toElem(IRState* p) 2617 DValue* FuncExp::toElem(IRState* p)