Skip to content

Commit

Permalink
Remove dead magics (nim-lang#13551)
Browse files Browse the repository at this point in the history
  • Loading branch information
krux02 authored Mar 3, 2020
1 parent 31332bc commit eb42f38
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 140 deletions.
34 changes: 16 additions & 18 deletions compiler/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ const
tagEffects* = 3 # user defined tag ('gc', 'time' etc.)
pragmasEffects* = 4 # not an effect, but a slot for pragmas in proc type
effectListLen* = 5 # list of effects list
nkLastBlockStmts* = {nkRaiseStmt, nkReturnStmt, nkBreakStmt, nkContinueStmt}
nkLastBlockStmts* = {nkRaiseStmt, nkReturnStmt, nkBreakStmt, nkContinueStmt}
# these must be last statements in a block

type
Expand Down Expand Up @@ -599,12 +599,11 @@ type
mDefined, mDefinedInScope, mCompiles, mArrGet, mArrPut, mAsgn,
mLow, mHigh, mSizeOf, mAlignOf, mOffsetOf, mTypeTrait,
mIs, mOf, mAddr, mType, mTypeOf,
mRoof, mPlugin, mEcho, mShallowCopy, mSlurp, mStaticExec, mStatic,
mPlugin, mEcho, mShallowCopy, mSlurp, mStaticExec, mStatic,
mParseExprToAst, mParseStmtToAst, mExpandToAst, mQuoteAst,
mUnaryLt, mInc, mDec, mOrd,
mInc, mDec, mOrd,
mNew, mNewFinalize, mNewSeq, mNewSeqOfCap,
mLengthOpenArray, mLengthStr, mLengthArray, mLengthSeq,
mXLenStr, mXLenSeq,
mIncl, mExcl, mCard, mChr,
mGCref, mGCunref,
mAddI, mSubI, mMulI, mDivI, mModI,
Expand All @@ -620,7 +619,7 @@ type
mEqEnum, mLeEnum, mLtEnum,
mEqCh, mLeCh, mLtCh,
mEqB, mLeB, mLtB,
mEqRef, mEqUntracedRef, mLePtr, mLtPtr,
mEqRef, mLePtr, mLtPtr,
mXor, mEqCString, mEqProc,
mUnaryMinusI, mUnaryMinusI64, mAbsI, mNot,
mUnaryPlusI, mBitnotI,
Expand All @@ -629,27 +628,27 @@ type
mStrToStr, mEnumToStr,
mAnd, mOr,
mEqStr, mLeStr, mLtStr,
mEqSet, mLeSet, mLtSet, mMulSet, mPlusSet, mMinusSet, mSymDiffSet,
mEqSet, mLeSet, mLtSet, mMulSet, mPlusSet, mMinusSet,
mConStrStr, mSlice,
mDotDot, # this one is only necessary to give nice compile time warnings
mFields, mFieldPairs, mOmpParFor,
mAppendStrCh, mAppendStrStr, mAppendSeqElem,
mInRange, mInSet, mRepr, mExit,
mInSet, mRepr, mExit,
mSetLengthStr, mSetLengthSeq,
mIsPartOf, mAstToStr, mParallel,
mSwap, mIsNil, mArrToSeq, mCopyStr, mCopyStrLast,
mSwap, mIsNil, mArrToSeq,
mNewString, mNewStringOfCap, mParseBiggestFloat,
mMove, mWasMoved, mDestroy,
mDefault, mUnown, mAccessEnv, mAccessTypeInfo, mReset,
mDefault, mUnown, mAccessEnv, mReset,
mArray, mOpenArray, mRange, mSet, mSeq, mOpt, mVarargs,
mRef, mPtr, mVar, mDistinct, mVoid, mTuple,
mOrdinal,
mInt, mInt8, mInt16, mInt32, mInt64,
mUInt, mUInt8, mUInt16, mUInt32, mUInt64,
mFloat, mFloat32, mFloat64, mFloat128,
mBool, mChar, mString, mCstring,
mPointer, mEmptySet, mIntSetBaseType, mNil, mExpr, mStmt, mTypeDesc,
mVoidType, mPNimrodNode, mShared, mGuarded, mLock, mSpawn, mDeepCopy,
mPointer, mNil, mExpr, mStmt, mTypeDesc,
mVoidType, mPNimrodNode, mSpawn, mDeepCopy,
mIsMainModule, mCompileDate, mCompileTime, mProcCall,
mCpuEndian, mHostOS, mHostCPU, mBuildOS, mBuildCPU, mAppType,
mCompileOption, mCompileOptionArg,
Expand All @@ -662,7 +661,7 @@ type
mNIntVal, mNFloatVal, mNSymbol, mNIdent, mNGetType, mNStrVal, mNSetIntVal,
mNSetFloatVal, mNSetSymbol, mNSetIdent, mNSetType, mNSetStrVal, mNLineInfo,
mNNewNimNode, mNCopyNimNode, mNCopyNimTree, mStrToIdent, mNSigHash, mNSizeOf,
mNBindSym, mLocals, mNCallSite,
mNBindSym, mNCallSite,
mEqIdent, mEqNimrodNode, mSameNodeType, mGetImpl, mNGenSym,
mNHint, mNWarning, mNError,
mInstantiationInfo, mGetTypeInfo,
Expand All @@ -673,9 +672,9 @@ type

# things that we can evaluate safely at compile time, even if not asked for it:
const
ctfeWhitelist* = {mNone, mUnaryLt, mSucc,
ctfeWhitelist* = {mNone, mSucc,
mPred, mInc, mDec, mOrd, mLengthOpenArray,
mLengthStr, mLengthArray, mLengthSeq, mXLenStr, mXLenSeq,
mLengthStr, mLengthArray, mLengthSeq,
mArrGet, mArrPut, mAsgn, mDestroy,
mIncl, mExcl, mCard, mChr,
mAddI, mSubI, mMulI, mDivI, mModI,
Expand All @@ -690,17 +689,16 @@ const
mEqEnum, mLeEnum, mLtEnum,
mEqCh, mLeCh, mLtCh,
mEqB, mLeB, mLtB,
mEqRef, mEqProc, mEqUntracedRef, mLePtr, mLtPtr, mEqCString, mXor,
mEqRef, mEqProc, mLePtr, mLtPtr, mEqCString, mXor,
mUnaryMinusI, mUnaryMinusI64, mAbsI, mNot, mUnaryPlusI, mBitnotI,
mUnaryPlusF64, mUnaryMinusF64,
mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr, mFloatToStr, mCStrToStr,
mStrToStr, mEnumToStr,
mAnd, mOr,
mEqStr, mLeStr, mLtStr,
mEqSet, mLeSet, mLtSet, mMulSet, mPlusSet, mMinusSet, mSymDiffSet,
mEqSet, mLeSet, mLtSet, mMulSet, mPlusSet, mMinusSet,
mConStrStr, mAppendStrCh, mAppendStrStr, mAppendSeqElem,
mInRange, mInSet, mRepr,
mCopyStr, mCopyStrLast}
mInSet, mRepr}

type
PNode* = ref TNode
Expand Down
34 changes: 3 additions & 31 deletions compiler/ccgexprs.nim
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,6 @@ proc binaryArith(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
of mLeB: applyFormat("($1 <= $2)")
of mLtB: applyFormat("($1 < $2)")
of mEqRef: applyFormat("($1 == $2)")
of mEqUntracedRef: applyFormat("($1 == $2)")
of mLePtr: applyFormat("($1 <= $2)")
of mLtPtr: applyFormat("($1 < $2)")
of mXor: applyFormat("($1 != $2)")
Expand Down Expand Up @@ -1797,7 +1796,7 @@ proc genInOp(p: BProc, e: PNode, d: var TLoc) =

proc genSetOp(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
const
lookupOpr: array[mLeSet..mSymDiffSet, string] = [
lookupOpr: array[mLeSet..mMinusSet, string] = [
"for ($1 = 0; $1 < $2; $1++) { $n" &
" $3 = (($4[$1] & ~ $5[$1]) == 0);$n" &
" if (!$3) break;}$n",
Expand All @@ -1807,8 +1806,7 @@ proc genSetOp(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
"if ($3) $3 = (#nimCmpMem($4, $5, $2) != 0);$n",
"&",
"|",
"& ~",
"^"]
"& ~"]
var a, b, i: TLoc
var setType = skipTypes(e[1].typ, abstractVar)
var size = int(getSize(p.config, setType))
Expand Down Expand Up @@ -1838,7 +1836,6 @@ proc genSetOp(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
of mMulSet: binaryExpr(p, e, d, "($1 & $2)")
of mPlusSet: binaryExpr(p, e, d, "($1 | $2)")
of mMinusSet: binaryExpr(p, e, d, "($1 & ~ $2)")
of mSymDiffSet: binaryExpr(p, e, d, "($1 ^ $2)")
of mInSet:
genInOp(p, e, d)
else: internalError(p.config, e.info, "genSetOp()")
Expand Down Expand Up @@ -1868,7 +1865,7 @@ proc genSetOp(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
initLocExpr(p, e[1], a)
initLocExpr(p, e[2], b)
putIntoDest(p, d, e, ropecg(p.module, "(#nimCmpMem($1, $2, $3)==0)", [a.rdCharLoc, b.rdCharLoc, size]))
of mMulSet, mPlusSet, mMinusSet, mSymDiffSet:
of mMulSet, mPlusSet, mMinusSet:
# we inline the simple for loop for better code generation:
getTemp(p, getSysType(p.module.g.graph, unknownLineInfo, tyInt), i) # our counter
initLocExpr(p, e[1], a)
Expand Down Expand Up @@ -2118,9 +2115,6 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
of mRepr: genRepr(p, e, d)
of mGetTypeInfo: genGetTypeInfo(p, e, d)
of mSwap: genSwap(p, e, d)
of mUnaryLt:
if optOverflowCheck notin p.options: unaryExpr(p, e, d, "($1 - 1)")
else: unaryExpr(p, e, d, "#subInt($1, 1)")
of mInc, mDec:
const opr: array[mInc..mDec, string] = ["+=", "-="]
const fun64: array[mInc..mDec, string] = ["addInt64",
Expand Down Expand Up @@ -2218,30 +2212,13 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
of mOrd: genOrd(p, e, d)
of mLengthArray, mHigh, mLengthStr, mLengthSeq, mLengthOpenArray:
genArrayLen(p, e, d, op)
of mXLenStr:
if not p.module.compileToCpp:
unaryExpr(p, e, d, "($1->Sup.len)")
else:
unaryExpr(p, e, d, "$1->len")
of mXLenSeq:
# see 'taddhigh.nim' for why we need to use a temporary here:
var a, tmp: TLoc
initLocExpr(p, e[1], a)
getIntTemp(p, tmp)
if not p.module.compileToCpp:
lineCg(p, cpsStmts, "$1 = $2->Sup.len;$n", [tmp.r, rdLoc(a)])
else:
lineCg(p, cpsStmts, "$1 = $2->len;$n", [tmp.r, rdLoc(a)])
putIntoDest(p, d, e, tmp.r)
of mGCref: unaryStmt(p, e, d, "if ($1) { #nimGCref($1); }$n")
of mGCunref: unaryStmt(p, e, d, "if ($1) { #nimGCunref($1); }$n")
of mSetLengthStr: genSetLengthStr(p, e, d)
of mSetLengthSeq: genSetLengthSeq(p, e, d)
of mIncl, mExcl, mCard, mLtSet, mLeSet, mEqSet, mMulSet, mPlusSet, mMinusSet,
mInSet:
genSetOp(p, e, d, op)
of mCopyStr, mCopyStrLast:
genCall(p, e, d)
of mNewString, mNewStringOfCap, mExit, mParseBiggestFloat:
var opr = e[0].sym
# Why would anyone want to set nodecl to one of these hardcoded magics?
Expand Down Expand Up @@ -2296,11 +2273,6 @@ proc genMagicExpr(p: BProc, e: PNode, d: var TLoc, op: TMagic) =
of mMove: genMove(p, e, d)
of mDestroy: genDestroy(p, e)
of mAccessEnv: unaryExpr(p, e, d, "$1.ClE_0")
of mAccessTypeInfo:
var a: TLoc
var dummy: Rope
initLocExpr(p, e[1], a)
putIntoDest(p, d, e, rdMType(p, a, dummy))
of mSlice:
localError(p.config, e.info, "invalid context for 'toOpenArray'; " &
"'toOpenArray' is only valid within a call expression")
Expand Down
2 changes: 1 addition & 1 deletion compiler/forloops.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import ast, astalgo

const
someCmp = {mEqI, mEqF64, mEqEnum, mEqCh, mEqB, mEqRef, mEqProc,
mEqUntracedRef, mLeI, mLeF64, mLeU, mLeU64, mLeEnum,
mLeI, mLeF64, mLeU, mLeU64, mLeEnum,
mLeCh, mLeB, mLePtr, mLtI, mLtF64, mLtU, mLtU64, mLtEnum,
mLtCh, mLtB, mLtPtr}

Expand Down
11 changes: 4 additions & 7 deletions compiler/guards.nim
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@ import ast, astalgo, msgs, magicsys, nimsets, trees, types, renderer, idents,

const
someEq = {mEqI, mEqF64, mEqEnum, mEqCh, mEqB, mEqRef, mEqProc,
mEqUntracedRef, mEqStr, mEqSet, mEqCString}
mEqStr, mEqSet, mEqCString}

# set excluded here as the semantics are vastly different:
someLe = {mLeI, mLeF64, mLeU, mLeU64, mLeEnum,
mLeCh, mLeB, mLePtr, mLeStr}
someLt = {mLtI, mLtF64, mLtU, mLtU64, mLtEnum,
mLtCh, mLtB, mLtPtr, mLtStr}

someLen = {mLengthOpenArray, mLengthStr, mLengthArray, mLengthSeq,
mXLenStr, mXLenSeq}
someLen = {mLengthOpenArray, mLengthStr, mLengthArray, mLengthSeq}

someIn = {mInRange, mInSet}
someIn = {mInSet}

someHigh = {mHigh}
# we don't list unsigned here because wrap around semantics suck for
Expand Down Expand Up @@ -258,7 +257,7 @@ proc canon*(n: PNode; o: Operators): PNode =
result[i] = canon(n[i], o)
elif n.kind == nkSym and n.sym.kind == skLet and
n.sym.astdef.getMagic in (someEq + someAdd + someMul + someMin +
someMax + someHigh + {mUnaryLt} + someSub + someLen + someDiv):
someMax + someHigh + someSub + someLen + someDiv):
result = n.sym.astdef.copyTree
else:
result = n
Expand All @@ -271,8 +270,6 @@ proc canon*(n: PNode; o: Operators): PNode =
of someHigh:
# high == len+(-1)
result = o.opAdd.buildCall(o.opLen.buildCall(result[1]), minusOne())
of mUnaryLt:
result = buildCall(o.opAdd, result[1], minusOne())
of someSub:
# x - 4 --> x + (-4)
result = negate(result[1], result[2], result, o)
Expand Down
14 changes: 2 additions & 12 deletions compiler/jsgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ type

const # magic checked op; magic unchecked op;
jsMagics: TMagicOps = [
["addInt", ""], # AddI
mAddI: ["addInt", ""],
["subInt", ""], # SubI
["mulInt", ""], # MulI
["divInt", ""], # DivI
Expand Down Expand Up @@ -414,7 +414,6 @@ const # magic checked op; magic unchecked op;
["", ""], # LeB
["", ""], # LtB
["", ""], # EqRef
["", ""], # EqUntracedRef
["", ""], # LePtr
["", ""], # LtPtr
["", ""], # Xor
Expand Down Expand Up @@ -588,7 +587,6 @@ proc arithAux(p: PProc, n: PNode, r: var TCompRes, op: TMagic) =
of mLeB: applyFormat("($1 <= $2)", "($1 <= $2)")
of mLtB: applyFormat("($1 < $2)", "($1 < $2)")
of mEqRef: applyFormat("($1 == $2)", "($1 == $2)")
of mEqUntracedRef: applyFormat("($1 == $2)", "($1 == $2)")
of mLePtr: applyFormat("($1 <= $2)", "($1 <= $2)")
of mLtPtr: applyFormat("($1 < $2)", "($1 < $2)")
of mXor: applyFormat("($1 != $2)", "($1 != $2)")
Expand Down Expand Up @@ -631,7 +629,7 @@ proc arith(p: PProc, n: PNode, r: var TCompRes, op: TMagic) =
of mCharToStr, mBoolToStr, mIntToStr, mInt64ToStr, mFloatToStr,
mCStrToStr, mStrToStr, mEnumToStr:
arithAux(p, n, r, op)
of mEqRef, mEqUntracedRef:
of mEqRef:
if mapType(n[1].typ) != etyBaseIndex:
arithAux(p, n, r, op)
else:
Expand Down Expand Up @@ -1890,10 +1888,6 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
of mAddI..mStrToStr: arith(p, n, r, op)
of mRepr: genRepr(p, n, r)
of mSwap: genSwap(p, n)
of mUnaryLt:
# XXX: range checking?
if not (optOverflowCheck in p.options): unaryExpr(p, n, r, "", "$1 - 1")
else: unaryExpr(p, n, r, "subInt", "subInt($1, 1)")
of mAppendStrCh:
binaryExpr(p, n, r, "addChar",
"if ($1 != null) { addChar($3, $2); } else { $3 = [$2]; }")
Expand Down Expand Up @@ -1959,8 +1953,6 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
of mOrd: genOrd(p, n, r)
of mLengthStr, mLengthSeq, mLengthOpenArray, mLengthArray:
unaryExpr(p, n, r, "", "($1 != null ? $2.length : 0)")
of mXLenStr, mXLenSeq:
unaryExpr(p, n, r, "", "$1.length")
of mHigh:
unaryExpr(p, n, r, "", "($1 != null ? ($2.length-1) : -1)")
of mInc:
Expand Down Expand Up @@ -2007,8 +1999,6 @@ proc genMagic(p: PProc, n: PNode, r: var TCompRes) =
of mEcho: genEcho(p, n, r)
of mNLen..mNError, mSlurp, mStaticExec:
localError(p.config, n.info, errXMustBeCompileTime % n[0].sym.name.s)
of mCopyStr:
binaryExpr(p, n, r, "", "($1.slice($2))")
of mNewString: unaryExpr(p, n, r, "mnewString", "mnewString($1)")
of mNewStringOfCap:
unaryExpr(p, n, r, "mnewString", "mnewString(0)")
Expand Down
10 changes: 1 addition & 9 deletions compiler/semfold.nim
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; g: ModuleGraph): PNode =
else:
result = newIntNodeT(bitnot(getInt(a)), n, g)
of mLengthArray: result = newIntNodeT(lengthOrd(g.config, a.typ), n, g)
of mLengthSeq, mLengthOpenArray, mXLenSeq, mLengthStr, mXLenStr:
of mLengthSeq, mLengthOpenArray, mLengthStr:
if a.kind == nkNilLit:
result = newIntNodeT(Zero, n, g)
elif a.kind in {nkStrLit..nkTripleStrLit}:
Expand All @@ -198,7 +198,6 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; g: ModuleGraph): PNode =
of mUnaryPlusI, mUnaryPlusF64: result = a # throw `+` away
# XXX: Hides overflow/underflow
of mAbsI: result = foldAbs(getInt(a), n, g)
of mUnaryLt: result = foldSub(getOrdValue(a), One, n, g)
of mSucc: result = foldAdd(getOrdValue(a), getInt(b), n, g)
of mPred: result = foldSub(getOrdValue(a), getInt(b), n, g)
of mAddI: result = foldAdd(getInt(a), getInt(b), n, g)
Expand Down Expand Up @@ -330,9 +329,6 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; g: ModuleGraph): PNode =
of mMinusSet:
result = nimsets.diffSets(g.config, a, b)
result.info = n.info
of mSymDiffSet:
result = nimsets.symdiffSets(g.config, a, b)
result.info = n.info
of mConStrStr: result = newStrNodeT(getStrOrChar(a) & getStrOrChar(b), n, g)
of mInSet: result = newIntNodeT(toInt128(ord(inSet(a, b))), n, g)
of mRepr:
Expand All @@ -342,10 +338,6 @@ proc evalOp(m: TMagic, n, a, b, c: PNode; g: ModuleGraph): PNode =
of mBoolToStr:
if getOrdValue(a) == 0: result = newStrNodeT("false", n, g)
else: result = newStrNodeT("true", n, g)
of mCopyStr: result = newStrNodeT(substr(getStr(a), int(toInt64(getOrdValue(b)))), n, g)
of mCopyStrLast:
result = newStrNodeT(substr(getStr(a), toInt(getOrdValue(b)),
toInt(getOrdValue(c))), n, g)
of mFloatToStr: result = newStrNodeT($getFloat(a), n, g)
of mCStrToStr, mCharToStr:
if a.kind == nkBracket:
Expand Down
4 changes: 1 addition & 3 deletions compiler/semmagic.nim
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ proc evalTypeTrait(c: PContext; traitCall: PNode, operand: PType, context: PSym)

if operand.kind == tyGenericParam or (traitCall.len > 2 and operand2.kind == tyGenericParam):
return traitCall ## too early to evaluate

let s = trait.sym.name.s
case s
of "or", "|":
Expand Down Expand Up @@ -479,8 +479,6 @@ proc magicsAfterOverloadResolution(c: PContext, n: PNode,
result.typ = n[1].typ
of mDotDot:
result = n
of mRoof:
localError(c.config, n.info, "builtin roof operator is not supported anymore")
of mPlugin:
let plugin = getPlugin(c.cache, n[0].sym)
if plugin.isNil:
Expand Down
6 changes: 0 additions & 6 deletions compiler/semtypes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -1898,10 +1898,6 @@ proc processMagicType(c: PContext, m: PSym) =
setMagicIntegral(c.config, m, tyCString, c.config.target.ptrSize)
rawAddSon(m.typ, getSysType(c.graph, m.info, tyChar))
of mPointer: setMagicIntegral(c.config, m, tyPointer, c.config.target.ptrSize)
of mEmptySet:
setMagicIntegral(c.config, m, tySet, 1)
rawAddSon(m.typ, newTypeS(tyEmpty, c))
of mIntSetBaseType: setMagicIntegral(c.config, m, tyRange, c.config.target.intSize)
of mNil: setMagicType(c.config, m, tyNil, c.config.target.ptrSize)
of mExpr:
if m.name.s == "auto":
Expand Down Expand Up @@ -1937,8 +1933,6 @@ proc processMagicType(c: PContext, m: PSym) =
incl m.typ.flags, tfHasAsgn
assert c.graph.sysTypes[tySequence] == nil
c.graph.sysTypes[tySequence] = m.typ
of mOpt:
setMagicType(c.config, m, tyOpt, szUncomputedSize)
of mOrdinal:
setMagicIntegral(c.config, m, tyOrdinal, szUncomputedSize)
rawAddSon(m.typ, newTypeS(tyNone, c))
Expand Down
Loading

0 comments on commit eb42f38

Please sign in to comment.